const regex = / \(\?P<(\w+)> # named group (name captured as group 1): (?P<name>
( # matching pattern as group (2) in form: b(R1)a1...(R2)a2
[^)(]*+ # `b` (before) part - any not `)(` characters
(?: # non capturing group for repeated `(R)a`
\((?2)\) # `R` - reference to group 2 for possible nested braces
[^)(]*+ # 'a' (after) part - any not `)(` characters
)* # `(R)a` repetition is optional
) # end the group 2
\) # end of the named group to match/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp(' \\(\\?P<(\\w+)> # named group (name captured as group 1): (?P<name>
( # matching pattern as group (2) in form: b(R1)a1...(R2)a2
[^)(]*+ # `b` (before) part - any not `)(` characters
(?: # non capturing group for repeated `(R)a`
\\((?2)\\) # `R` - reference to group 2 for possible nested braces
[^)(]*+ # \'a\' (after) part - any not `)(` characters
)* # `(R)a` repetition is optional
) # end the group 2
\\) # end of the named group to match', 'g')
const str = `(?P<kind>(frames_cleanpass)|(disparity))/(?P<subset>\\w+)/(?P<scene_1>\\w+)/(?P<scene_2>\\w+)/(?P<side>\\w+)/(?P<scene_3>\\w+)\\.(?P<ext>(?(3)pfm|png))`;
const subst = `{\1}`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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