const regex = /^(?:(?=\S*\d)\S+(?:\s+(?=\S*\d)\S+)*\W*)?\K\S*/gmu;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?:(?=\\S*\\d)\\S+(?:\\s+(?=\\S*\\d)\\S+)*\\W*)?\\K\\S*', 'gmu')
const str = `First12word50 Secųond-Word Thirdųword ' result must be Secųond-Word
First1-2word50 Secųond/Word Thirdųword ' result must be Secųond/Word
First1/2word50 Secųond+Word Thirdųword ' result must be Secųond+Word
First1/2word50 Secųond1+Word Thirdų-word ' result must be Thirdų-word
First1/2word50 Sec1ųond1+Word Thir11dų-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