1const telephoneCheck = str => {
2 const regExp = /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm
3 return regExp.test(str)
4}
5
6telephoneCheck("27576227382");
7
8 /* Regular Expression Explanation Below !!! */
9
10/*
11 Expression : /^1?\s?(\(\d{3}\)|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm
12
13 ^ asserts position at start of a line
14 1? matches the character 1 literally (case sensitive)
15 ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
16 \s? matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
17 ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
18 1st Capturing Group (\(\d{3}\)|\d{3})
19 1st Alternative \(\d{3}\)
20 \( matches the character ( literally (case sensitive)
21 \d{3} matches a digit (equal to [0-9])
22 {3} Quantifier — Matches exactly 3 times
23 \) matches the character ) literally (case sensitive)
24 2nd Alternative \d{3}
25 \d{3} matches a digit (equal to [0-9])
26 {3} Quantifier — Matches exactly 3 times
27 2nd Capturing Group (\s|-)?
28 ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
29 1st Alternative \s
30 \s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
31 2nd Alternative -
32 - matches the character - literally (case sensitive)
33 \d{3} matches a digit (equal to [0-9])
34 {3} Quantifier — Matches exactly 3 times
35 3rd Capturing Group (\s|-)?
36 ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
37 1st Alternative \s
38 \s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
39 2nd Alternative -
40 \d{4} matches a digit (equal to [0-9])
41 {4} Quantifier — Matches exactly 4 times
42 $ asserts position at the end of a line
43 Global pattern flags
44 g modifier: global. All matches (don't return after first match)
45 m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
46*/
47
48// With love @kouqhar