const regex = /((8|\+7?)[\- ]?)?(\(?\d{3}\)?[\- ]?)?([\d\- ]{7,10})/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((8|\\+7?)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?([\\d\\- ]{7,10})', 'g')
const str = `======== local phone
765-43-21
7654321
765-4321
======== operator phone
(921) 765-43-21
(921) 7654321
(921) 765-4321
(921)765-43-21
(921)7654321
(921)765-4321
921765-43-21
9217654321
921765-4321
921-765-43-21
921-7654321
921-765-4321
======== world phone
+7 (921) 765-43-21
+7(921) 765-43-21
+7-(921) 765-43-21
7 (921) 765-43-21
7(921) 765-43-21
8 (921) 765-43-21
8(921) 765-43-21
8-(921) 765-43-21
8 (921) 765-43-21
8(921) 765-43-21
8-(921) 765-43-21
+7 9217654321
+79217654321
+7-9217654321
7 9217654321
79217654321
7-9217654321
8 9217654321
89217654321
8-9217654321
8 9217654321
89217654321
8-9217654321
======== not found
>>>>>>>>> 1. Коржев Артём Борисович 8-921-641-82-15;
======== bad
0 0 0 0
333 000
1 1 1205 1320 321
======== разбор
# world phone
(
(8|\\+?7)
[\\- ]?
)?
# operator phone
(
\\(?
\\d{3}
\\)?
[\\- ]?
)?
# local phone
[\\d\\- ]{7,10}`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions