Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove () and - and white spaces from phone number in Javascript [closed]

I have a phone number like (123) 456-7891. I need number like 1234567891.

How can I do that in Javascript ?

like image 772
user2736812 Avatar asked Oct 18 '13 06:10

user2736812


3 Answers

To be on the safe side, I would recommend removing everything except + (for country codes) and digits:

result = subject.replace(/[^+\d]+/g, "");
like image 112
Tim Pietzcker Avatar answered Oct 23 '22 02:10

Tim Pietzcker


You can use String.replace() to do this. Pass it a RegExp that matches everything but digits and replace them with ''.

var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, ''); 
like image 32
nkron Avatar answered Oct 23 '22 02:10

nkron


alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));

like image 2
u_mulder Avatar answered Oct 23 '22 01:10

u_mulder