How to detect phone number on multiline using Regex?
I use this regular expression to detect 10 digits phone number:
/[0-9]{10}|[0-9]{3}.[0-9]{7}/gs
It detects things like: 050-1231231
, 0501231231
and even: 050
and 1231231
I would like to detect the number even if it's across 10 lines like so:
0
5
0
1
2
3
1
2
3
1
Solution 1:
Use
/\d(?:\W*\d){9}/g
See regex proof.
NOTE
If phone number is meant replace \W
with [-+()\s]
, and/or add any other characters you see in your phone numbers.
EXPLANATION
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
(?: group, but do not capture (9 times):
--------------------------------------------------------------------------------
\W* non-word characters (all but a-z, A-Z, 0-
9, _) (0 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
){9} end of grouping