Remove special characters like space,bracket,plus symbol,hyphen from phone number string

86 views Asked by At

I am considering below cases to remove garbage values ,or space bracket from a telephone number string.

Below are the examples I am considering.

//Ignore Special char
phone = "+1-555-555-1234"
result = "15555551234"

phone = "1(800)555-0199"
result = "18005550199"

//Ignore Space
phone="555 555 1234"
result = "5555551234"


//If phone number contains spaces,parenthesis or some garbage character only consider digits.
phone = "9898871234567)"
result = "9898871234567"

//If + and parenthesis symbol is present
phone = "+11 111 111"
result = "11111111"

phone= "(22) 222-222"
result = "22222222"

//If hyphen is present
phone = "(22) 222-222"
result = "22222222"

How can I easliy achive this in javascript using regex?

1

There are 1 answers

9
Steve Tomlin On

const result = '11-11+11(33)'.replace(/\D/g, '');
// Remove any character that is NOT a number.
console.log(result)