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
403 Forbidden When I Try to Post to My Spring API
Spring Rest - Create Zip File and Send It to the Client
How to Display Auto-Configuration Report When Running a Spring Boot Application
How to Get the Average of a 2D Array
Setonclicklistener() on a Null Object Reference Error
Error: Incompatible Types: Int[][] Cannot Be Converted to Int
How to Check If a Json Key Exists
Illegal Character When Trying to Compile Java Code
How to Make a Multipart/Form-Data Post Request Using Java
How to Call Methods Simultaneously in Java
Enter Array Without Knowing Its Size
Java: How to Check If Object Is Null
How to Test Code Dependent on Environment Variables Using Junit
How to Check Json in Response Body With Mockmvc
Java Coding Converted to Pseudo Code
Java Socket Api: How to Tell If a Connection Has Been Closed