const regex = /(Order[0-9])(?!.*\1)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(Order[0-9])(?!.*\\1)', 'g')
const str = `<ctx>
<PostCode>XXXXXX</PostCode>
<Title1>Mr</Title1>
<Name>John</Name>
<Order1>£100.00</Order1>
<Order2>£100.01</Order2>
<Order3>£100.01</Order3>
<Order4>£100.01</Order4>
<Order5>£100.01</Order5>
<Order6>£100.01</Order6>
<Order7>£100.01</Order7>
<Order8>£100.01</Order8>
<Order9>£100.01</Order9>
<Order10>£100.01</Order10>
<Order11>£100.01</Order11>
<Order50>£100.01</Order50>
<Date>10/10/2010</Date>
</ctx>`;
// 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