Remove Parentheses, Dashes, and Spaces from Phone Number

How to remove spaces & brackets from a phone number with Regex?

As other people said in the comments, your provided regular expression will remove the spaces for you but if you want to remove parentheses as well you need another regex for that. Keep in mind replace() method will return a new string so in order to get the new value and use it you should assign it to a new variable.

So your code should something like this:

const phone = '+7(999) 999 99 99'
let newPhone = phone.replace(/\s/g, '').replace(/[()]/g, '')
console.log(newPhone)
// or simply do this with one replace:
newPhone = phone.replace(/[()\s]/g, '')
console.log(newPhone)

Remove dash from a phone number

phoneNumber.replaceAll("[\\s\\-()]", "");

The regular expression defines a character class consisting of any whitespace character (\s, which is escaped as \\s because we're passing in a String), a dash (escaped because a dash means something special in the context of character classes), and parentheses.

See String.replaceAll(String, String).

EDIT

Per gunslinger47:

phoneNumber.replaceAll("\\D", "");

Replaces any non-digit with an empty string.

Removing spaces, hyphen and brackets from a string

This will do your job:

var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';

var result = str.replace(/[- )(]/g,'');

alert(result);

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

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, '');

SQL - Remove Parenthesis from Phone Number

Nest don't add

Replace(Replace(HomePhone,')',''),'(','')

Look at how the function replace works. It expects string With Text To Evaluate, string to replace, string to replace with)

By adding them you should be getting the number listed twice, but if the data type isn't long enough it may be truncating values. by nesting you're telling the system to replace the ) and then use that string w/o the ) to have the ( replaced with ''.



Related Topics



Leave a reply



Submit