Chong We Tan
Chong We Tan

Reputation: 243

Add specific special characters parenthesis ( ) in Regex

Currently, I have this regex for telephone number

/^\+?\d+(-\d+)*$/

which allows:

123
123-456
123-456-789

I'm aiming to also accept parenthesis in my regex ( ) for example:

(02)123-456

How can I do it with my current regex?

Upvotes: 0

Views: 64

Answers (1)

Dimitar
Dimitar

Reputation: 4783

Add/Append (\(\d+\))? in front of /^\+?\d+(-\d+)*$/

If you need min. 2 numbers, use this (\(\d{2,}\))?

Basically, you need to escape the (), remember that in almost any language you would do it with the \-character. From then on you only need to specify the number of occurrences.

Upvotes: 1

Related Questions