Suggest a regular expression to find a mobile phone number in the text.
Phone numbers are entered by different users and in a completely unpredictable format, for example, – + () _ spaces can participate and it is not yet known what the imagination will be enough for.
Phone examples:
8 900 000-00-00
+7 (900) 000 00-00
+7 9001112233
89001112233
8 (900) 111-2233
etc
Answer 1, authority 100%
I think something like this:
/ (\ + 7 | 8) [- _] * \ (? [- _] * (\ d {3 } [- _] * \)? ([- _] * \ d) {7} | \ d \ d [- _] * \ d \ d [- _] * \)? ([- _] * \ d) {6}) / g
console.log (
"8 900 000-00-00 \
+7 (900) 000 00-00 \
+7 9001112233 \
89001112233 \
8 (900) 111-2233 \
8 (1234) 12 12 12 \
8 (12-34) 12 12 12 \
8 (123) 412 12 12 ".match (
/ (\ + 7 | 8) [- _] * \ (? [- _] * (\ d {3} [- _] * \)? ([- _] * \ d) {7} | \ d \ d [- _] * \ d \ d [- _] * \)? ([- _] * \ d) {6}) / g
))
. as-console-wrapper.as-console-wrapper {max-height: 100vh}
Answer 2, authority 92%
Alternatively:
/ (?: \ + | \ d) [\ d \ - \ (\)] {9,} \ d / g
- Starts with: “+” or numbers
- Then may contain: numbers, “-“, “(“, “)”, space. Repeating 9 or more times
- Ends with digit
https://regex101.com/r/poJz8C/5
Answer 3, authority 25%
/ (?: \ + | \ d) [\ d \ - \ (\)] {9,} \ d / g
this is better
Answer 4
[0-9 | \ | \ - | \ (| \) | \ +] {10,17} [0 -9 | \ | \ - | \ (| \)]
Answer 5
You can choose a simple and understandable version yourself here https://regex101.com/
or like this:
/ (^ [7 | 8] {0,1} \ d {10} $) | (^ \ + 7 {1} \ d {10} $) /
where
^
is the beginning of the line.
$
is the end of the line.
[7 | 8] {0,1}
– 7 or 8 can be 1 time or 0 times.
\ d {10}
– numbers must be repeated 10 times.
|
– sign or.
(^ \ + 7 {1} \ d {10} $)
– similar here