This is a textbook example of where we need a backslash to escape a special character so the regular expression treats it as literal input.
As we’ve repeatedly seen, parentheses are special characters in regular expressions, but in this case we want to allow a user to enter parentheses and have our regex recognize them.
Explanation \d = only digit allow = length All phone numbers must in “xxx-xxxxxxx” format.
Character classes allow you to match any one out of a set of characters.
is another character class, one that allows any one of three separators.
When a question mark follows an unescaped left parenthesis like this, it’s not a quantifier, but instead helps to identify the type of grouping.
Standard capturing groups require the regular expression engine to keep track of backreferences, so it’s more efficient to use noncapturing groups whenever the text matched by a group does not need to be referenced later.