const regex = new RegExp('(?<!\\S)(?:(?=(-*\\d-*){7}(\\s|\\Z))[\\d-]+|(?=(-*\\d-*){11}(\\s|\\Z))[\\d-]+|(?=(-*\\d-*){12}(\\s|\\Z))[\\d-]+|(?=(-*\\d-*){13}(\\s|\\Z))[\\d-]+)', 'gm')
const str = `In the US 555-0198 and 1-206-5705-0100 are examples fictitious numbers.
In the UK, 044-113-496-1834 is a fictitious number.
In Ireland, the number 353-020-917-1234 is fictitious.
And in Australia, 061-970-654-321 is a fictitious number.
311 is a joke.`;
// 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