How to insert a space in phone numbers using javascript 2022

published: 11 Oct 2022

2 min read | website: https://stackoverflow.com/questions/25121734/split-and-add-space-between-phone-number

How to insert a space in phone numbers using javascript


Credit goes to Karl-André Gagnon, slightly modified to make it useful for us


// land lines
var string = "0276861111"
var phone = string.replace(/(\d{2})(\d{4})(\d{4})/, '$1 $2 $3');
console.log(phone); // "02 7686 1111"

//mobile phone numbers
var string = "0417123456"
var phone = string.replace(/(\d{4})(\d{3})(\d{3})/, '$1 $2 $3');
console.log(phone); //"0417 123 456"

How to insert a space in phone numbers using javascript 2022 | Coding Tips And Tricks
code by: Karl-André Gagnon

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming

TipsAndTricksta

I love coding, I love coding tips & tricks, code snippets and javascript