const regex = /^(?!.*(?: |^)(?:\+\d|0)(?:[()-]*\d){6,}(?!\S)).*/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?!.*(?: |^)(?:\\+\\d|0)(?:[()-]*\\d){6,}(?!\\S)).*', 'gm')
const str = `abcd1234567
1234567
123-4567
02221234567
0222-1234567
+862221234567
+86-222-123-4567
+86(222)1234567
86-222-1234567
r 1234567 er
e 123-4567 e
ewq 02221234567 qw
2312 0222-1234567 232
we +862221234567 23
231 +86-222-123-4567 ewe
,,,, +86(222)1234567 sss
ff 86-222-1234567 dd
Some string here with no phone number
a number with 7 and more digits is input
and it starts with 0 or + symbols
and could contain optional symbols "-", "(" and ")"
also enforce whitespace boundaries`;
// 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