2

I need to find those strings:

'555-555-5555'

'(555)555-5555'

I wrote:

number = '(555)555-5555'
regex =   ((/^\d{3,3}-\d{3,3}-\d{4,4}$/) | (/^\(\d{3,3}\)\d{3,3}-\d{4,4}$/))
let s =  number.match(regex)

But it returns 'null'. I don't understand, how use operator 'or' in regex here?

2
  • 1
    If you're trying to validate phone numbers, regex is generally the wrong tool for the job. Commented Aug 3, 2019 at 14:45
  • 3
    Try console.log(regex). You are using the Bitwise OR (|) operator and not alternation Commented Aug 3, 2019 at 14:49

1 Answer 1

3

You need to use to place the / delimiters outside of the regex. you can also simplify the quantifiers {3,3} by writing simply {3}.

This should give you the expected result

/^(?:\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/gm
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.