const regex = /(?<![0-9])([0-9]{1,3}?)([0-9]{3})([0-9]{3})(?![0-9]+?)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<![0-9])([0-9]{1,3}?)([0-9]{3})([0-9]{3})(?![0-9]+?)', 'gm')
const str = `Lorem 100 ipsum dolor 1000 sit 200 amet, 2000 consectetur adipiscing elit, sed 20000 do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in 300 voluptate 3000 velit esse 30000 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 400 cupidatat 4000 non proident, 40000 sunt in culpa qui 100000 officia 200000 deserunt mollit 300000 anim id 1234567 est 12345678 la 123456789 borum 123456789 ad more 1234567891 foo.`;
// 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