const regex = /\$(?P<type>(?:v|a|s))?(?P<prop>(?:lang|lang2|lang3|language|codec|format|ch|channels|res|resolution))(?:(?:_(?P<num>\d+))|(?:_(?P<lang>[^_0-9\$\n]+)(?:_(?P<num2>\d+))?))?\$/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\$(?P<type>(?:v|a|s))?(?P<prop>(?:lang|lang2|lang3|language|codec|format|ch|channels|res|resolution))(?:(?:_(?P<num>\\d+))|(?:_(?P<lang>[^_0-9\\$\\n]+)(?:_(?P<num2>\\d+))?))?\\$', 'gmi')
const str = `\$alang\$
\$alang_0\$
\$acodec\$
\$acodec_0\$
\$acodec_jp\$
\$acodec_jp_0\$
\$aformat\$
\$aformat_0\$
\$aformat_jp\$
\$aformat_jp_0\$
\$ach\$
\$ach_0\$
\$ach_jp\$
\$ach_jp_0\$
\$achannels\$
\$achannels_0\$
\$achannels_jp\$
\$achannels_jp_0\$
\$codec\$
\$res\$ \$vformat\$ \$aformat_jp\$ \$ach_0\$`;
// 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