Not everyone write numbers the same way. Some use sapaces, dashes, dots, and more. You can't find all numbers by just clicking find on page. Thas where RegExr comes in to help.
I have created an expression to find all US phone numbers on the page. A greate resource to help check your expression would be RegExr. Below I will have instruction on how it was built and works.
/(\+1[ -\d])?(1?)([ -\.]?)(\(?\d{3,5}[ -\.\)]?)[ -\.]?(\d{3}[ -\.]?)(\d{4})/g
- Character Classes
- Anchors
- Quantifiers
- Grouping Constructs
- Bracket Expressions
- The OR Operator
- Flags
- Test
If the expression has the symbol you must use a \ (backslash) \ before to use as a character. Except for - Character Classes
- The \d matches a digit
- The \w matches a word
- The \s matchaes a space
- The . is a wild card.
Anchors are not in my expression but they are good to know.
^
for the start of a string pattern$
for the ends of a string pattern
I use 3 of the 4 Quantifiers.
-
*
will find any characters starting with the first letter.- at* will find c
at
, dog, lat
itude, mat
, da
d.
- at* will find c
-
?
will find if a character is matching.(\+1[ -\d])?
will find any number that starts with +1 with the?
choseing one the the character in [ ]. Like+1
5551234,+1-
555-1234,+13
47-555-5555
-
{}
matches length. -
+
matches only single letter.- to use as a symbol you need to use a back slash
\+
- to use as a symbol you need to use a back slash
Grouping is used with ()
Groups;
(\+1[ -\d])?
(1?)
([ -\.]?)
(\(?\d{3,5}[ -\.\)]?)
([ -\.]?)
(\d{3}[ -\.]?)
(\d{4})
[]
will match any single character inside.[ -\.\)]
space,-
,.
,)
will be chosen.
|
will either left or right.ab | de
which isab
orde
/.../g
-Gloabl search/aBc/i
-Case Insensitive matches AbC/.../m
- Multiline
/.../gim
-Flags can be added together.
-
Writen by James Dobbs.
-
Please checkout my GitHub Dobbs96