How to prevent the regex from taking whitespaces

49 Views Asked by At

I was making a regular expression which can have the phone number of any length and can have the ( + and ) anywhere in the number.

^[\s()+-]*([0-9][\s()+-]*){6,20}$

But this regular expression is taking the spaces in it which is not correct. can someone help me to change this?

1

There are 1 best solutions below

4
RomanPerekhrest On

Remove character class \s from regex pattern to avoid matching whitespaces. Also escape parentheses () with backslashes as they are metacharacters:

^[()+-]*([0-9][()+-]*){6,20}$