const regex = /(?>^|\s+|\W)
(
(?>[a-z]\d*|\d+[a-z]?
(?>\(
([a-z]\d*|\d+[a-z]?)
\)[a-z]?
)?)
(?>\.(?>[a-z]|\d+[a-z]?))*
|
(?>M{0,4}(?>CM|CD|D?C{0,3})(?>XC|XL|L?X{0,3})(?>IX|IV|V?I{0,3}))
)
(?>\s+|\W|$)/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?>^|\\s+|\\W)
(
(?>[a-z]\\d*|\\d+[a-z]?
(?>\\(
([a-z]\\d*|\\d+[a-z]?)
\\)[a-z]?
)?)
(?>\\.(?>[a-z]|\\d+[a-z]?))*
|
(?>M{0,4}(?>CM|CD|D?C{0,3})(?>XC|XL|L?X{0,3})(?>IX|IV|V?I{0,3}))
)
(?>\\s+|\\W|$)', 'gmi')
const str = `Appendix 1
6.48
CP 198
3.2.4 G
2
1.
B.
Appendix 1
Annex A
4A.10.1 and 4A.15
4A.21.10
Annex ii
A)
SECTION IV
52.19A
PRACTICE DIRECTION 83—
RSC ORDER 109
Rule 4
ADMINISTRATIVE COURT GUIDANCE
PRACTICE STATEMENT
THE PROTOCOL
C.6
Draft modules
CIS11:
Appendix 13(2)e
2002 no.
(a)
L7.3.1.R
(iii)
`;
// 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