const regex = /^(counter|histogram)[.]_*[a-z]+[a-z_0-9]*[.]_*[a-z]+[a-z_0-9]*/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(counter|histogram)[.]_*[a-z]+[a-z_0-9]*[.]_*[a-z]+[a-z_0-9]*', 'gm')
const str = `counter..
counter. .
counter.a.b
counter._._
counter._a._
counter._._a
counter._a._a
counter._1._1
counter._a1._a1
counter.system.crashes
counter.system_server.crashes
counter.system_server2.crashes_
counter.system_server2._crashes_
counter.system_server2._crashes_1
counter.system_server2._crashes_2
counter.2system_server.2crashes
counter.2system_server.crashes2
counter.system_server.crashes_2
counter.system_server._crashes_2
counter.system_server.2_crashes_2
counter.scheduler.jobs_delayed
counter.scheduler.jobs_delayed2
counter.scheduler_.jobs_delayed_
counter.scheduler._jobs_delayed_
counter.scheduler.jobs_delayed_
histogram.scheduler.jobs_delayed`;
// 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