const regex = /(\+?6)?\ ?0\ ?\d{1,2}\-?\s?\d{3,4}\s?\d{4}/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\+?6)?\\ ?0\\ ?\\d{1,2}\\-?\\s?\\d{3,4}\\s?\\d{4}', 'gm')
const str = `03-2222 3333
04-222 3333
010-222 3333
011-2222 3333
011 2222 3333
+60 3-2222 3333
+60 4-222 3333
+60 10-222 3333
+60 11-2222 3333
+60 11 2222 3333
+6011 2222 3333
+6011-2222 3333
60102223333
601122223333
0102223333
01122223333
+60322223333
+6042223333
call 012-0000 0000 now`;
// 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