0

I want to use the below regex in my JS that is already working on a "phone number" form field in my company's application.

When I attempt to put it into Regex101's playground and type in 777-777-7777 I cannot get a match. It makes no sense. Anyone know what the issue can be? I am just trying to gain a better understanding of regular expressions as a junior developer.

[2-9]\\d{2}(-){0,1}[2-9]\\d{2}(-){0,1}\\d{4}$
Dean Friedland
  • 773
  • 2
  • 12
  • 32
  • If you read regex 101's description of your regex (over on the right side), you would see that it says *\\ matches the character \ literally*. By the way, `{0,1}` can be written as `?`. Also, don't you want to anchor this at the beginning with `^`? –  Jan 26 '17 at 17:41
  • Thanks. That is awesome. Just added the ? – Dean Friedland Jan 26 '17 at 18:17

1 Answers1

0

In Javascript regex, you must escape the backslashes (\), so your regex looks like [2-9]\\d{2}(-){0,1}[2-9]\\d{2}(-){0,1}\\d{4}$.

But for Regex101, you must unescape them, so your regex is [2-9]\d{2}(-){0,1}[2-9]\d{2}(-){0,1}\d{4}$

As a sidenote: do you really mean to capture the -?

Bruno Ferreira
  • 1,621
  • 12
  • 19