const regex = /(?:{{(?:(\d?\-?\d?)(:=?)('[^']*'),*).*?}})/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:{{(?:(\\d?\\-?\\d?)(:=?)(\'[^\']*\'),*).*?}})', 'g')
const str = `0:'ingen',1:'menneske',2:'mennesker'
0:='ingen',-1:'fugl'
1:'cola',2-:'colar'
0:='ingen',0:'biler',1:'bil',2:'biler',3-:'bøller'
{{0:'ingen',1:'menneske',2:'mennesker'}}
{{0-2:='ingen',0-1:'fugl'}}
{{1:'cola',2-:'colar'}}
{{0:='ingen',0:'biler',1:'bil',2:'biler',3-:'bøller'}}
Single \${{var}}
Vi er \${{var_U}}{{0:'ingen',1:'menneske',2:'mennesker'}}
Der er en \${{var_1}}{{0:='ingen',0-1:'fugl'}}
Gi' mig \${{var}}{{1:'cola',2-:'colar'}}
Jeg har \${{var}}{{0:='ingen',0:'biler',1:'bil',2:'biler',3-:'bøller'}}
\\\$\\{\\{(.+)\\}\\}\\{\\{((\\d)(\\-)?(\\d)?(:(=)?)\\'(.+)\\'),?\\}\\}
\\\${{([A-Za-z\\_\\d]+)}}((?:{{)?((\\d-?\\d?)(:=?)\\'(.*?)\\'),?(?:}})?)?
\\\${{(?P<var>[A-Za-z09_]+)}}({{(?:(?P<factorL>\\d)?(?P<diff>\\-)?(?P<factorH>\\d)?(?P<assert>:=?)(\\'(?P<string>.+?)\\')+?)?}})?
{{([A-Za-z0-9_]+)}}(?:{{((\\d?\\-?\\d?)(:=?)('.+?'),?)+}})?`;
// 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