const regex = /\bch.*?\K\b(?:\d+|VI{0,3}|I(?:[XV]|I{0,2}))\b/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\bch.*?\\K\\b(?:\\d+|VI{0,3}|I(?:[XV]|I{0,2}))\\b', 'gmi')
const str = `"chapter some word 12"
"chapter some word 12 some word"
"chapter some word I some word"
"chapter some word II some word"
"chapter some word III some word"
"chapter some word iv some word"
"chapter some word v some word"
"chapter some word vi some word"
"chapter some word vii some word"
"chapter some word viI some word"
"chapter some word ix some word"
"chapter some word x some word"
"chapter some word xi some word"
`;
// 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