const regex = /^(0|\+98)9[0-9]{9}$/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(0|\\+98)9[0-9]{9}$', 'gmi')
const str = `09156987452
09908472853
09153610882365
09158645287
+9814756842
+980915874622
+989908472853
+981283547
9836587498
+986859554
+989158497858
+9812457868
02154091563214587
12409156032145
091596356478
+982654789652
023654d
5588
879 091586424569
+9878546
+9999999999999
+989153610883
0915603551555555555
`;
// 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