const regex = /(\(?(0{1,2}|\+)\d{1,2}\)?)?(([ -]*\d+){8,20})+/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\(?(0{1,2}|\\+)\\d{1,2}\\)?)?(([ -]*\\d+){8,20})+', 'gm')
const str = `
https://asd.com/20441235534-aaaaaaaaaaa-
202460676
aasdasd 202460676
https://asd.com/20441235534
202460676-
10 text
1234 text
text 123
00491234567890
+491234567890
0123-4567890
0123 4567 789
0123 456 7890
0123 45 67 789
+490123 4567 789
+490123 456 7890
+49 123 45 67 789
123 4567 789
123 456 7890
123 45 67 789
+49 1234567890
+491234567890
0049 1234567890
0049 1234 567 890
(0049)1234567890
(+49)1234567890
(0049) 1234567890
(+49) 1234567890
text text (0049) 1234567890 text text
text text (+49) 1234567890 text text`;
// 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