Regex question for Telephone Number Validator

All the statements below worked on https://regex101.com.

The first one had an error and looks like js doesn’t support lookbehinds.

/^(?:1\s|1)?(\(?)\d{3}(?(?<=\(\d{3})\)\s?\d{3}-\d{4}|(?:\d{7}|-\d{3}-\d{4}|\s\d{3}\s\d{4}))$/

I refactored it without lookbehinds:

/^(?:1\s|1)?(?(?=\(\d{3}\))\(\d{3}\)\s*\d{3}-\d{4}|(\d{3}(.)\d{3}\2\d{4}|\d{10}))$/

Again, I put this into regex101 and it worked.

Here’s what I got from the console:

SyntaxError: Invalid regular expression: /^(?:1\s|1)?(?(?=\(\d{3}\))\(\d{3}\)\s*\d{3}-\d{4}|(\d{3}(.)\d{3}\2\d{4}|\d{10}))$/: Invalid group

When I put your expression into regex101 it identifies the error to a specific ?.
See here:

I was using this pattern:

(?(?=regex)then|else) 

that I got from https://www.regular-expressions.info/conditional.html
but it wanted:

((?=regex)then|else) 

A super long regex like that is def inferior to an array of manageable regex expressions to check against for production but it was very fun getting into the nitty_gritty. :slight_smile: