Regex Expressions

Here are some of the regex expressions that I’ve found useful. The first is just for US phone numbers. I’ve modified one that I found so that it is a little more restrictive, specifically not allowing zeros or ones for the first digit of the area code or the central office code.

/^\(?([2-9][0-9]{2})[-\s\).]?([2-9][0-9]{2})[-\s.]?([0-9]{4})$

Here’s how to format it:

($1)●$2-$3

Source: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html

I’ve also used https://regex101.com/ for testing and decoding them.

Leave a Comment





This site uses Akismet to reduce spam. Learn how your comment data is processed.