DEV Community


Posted on

How to extract phone number digits from a string?

We have a string that contains a phone number.
To extract digits we use the replace method.
We replace all symbols that are not digits with the empty string.
All that remains is a phone number.

let string = 'mob. phone: +1 (234) 56-78-901';
let phoneNumber = string.replace(/\D/g, '');
console.log(phoneNumber, string);
Enter fullscreen mode Exit fullscreen mode

That’s all.

I hope you found this article useful, if you need any help please let me know in the comment section.

πŸ‘‹ See you next time. Have a nice day!

Subscribe to our channel:

Discussion (0)