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.
Since a question mark is used after each separator, the phone number digits are allowed to run together. The NANP is the telephone numbering plan for the countries that share the country code “1.” This includes the United States and its territories, Canada, Bermuda, and 17 Caribbean nations.
It excludes Mexico and the Central American nations.
Regex class for validate any input string for any specific format.
is another character class, one that allows any one of three separators.
It’s important that the hyphen appears first or last in this character class, because if it appeared between other characters, it would create a range, as with .
Any quantifier that allows something to match zero times effectively makes that element optional.
A regular expression can be a single character, or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text replace operations.