const regex = /(?(DEFINE)
(?<tagnames> @CUSTOM-AT-1 | @CUSTOM-AT-2)
(?<number> -? (?= [1-9]|0(?!\d) ) \d+ (\.\d+)? ([eE] [+-]? \d+)? )
(?<boolean> true | false | null )
(?<string> \" ([^\"\\\\]* | \\\\ [\"\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* \" )
(?<array> \[ (?: (?&json) (?: , (?&json) )* )? \s* \] )
(?<pair> \s* (?&string) \s* : (?&json) )
(?<object> \{ (?: (?&pair) (?: , (?&pair) )* )? \s* \} )
(?<fieldname> [a-zA-Z0-9\_\-]+ )
(?<json> \s* (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) )
(?<fieldlist> (?: (?&fieldname) (?: , (?&fieldname) )+ )+ ) )
(?<actiontag> (?&tagnames)
)
(?:\=
(?'params'
(?:
(?'match_list'(?&fieldlist))
|
(?'match_json'(?&json))
|
(?'match_string'(?:[[:alnum:]\_\-]+))
)
)
)/mig;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?(DEFINE)
(?<tagnames> @CUSTOM-AT-1 | @CUSTOM-AT-2)
(?<number> -? (?= [1-9]|0(?!\\d) ) \\d+ (\\.\\d+)? ([eE] [+-]? \\d+)? )
(?<boolean> true | false | null )
(?<string> \\" ([^\\"\\\\\\\\]* | \\\\\\\\ [\\"\\\\\\\\bfnrt\\\/] | \\\\\\\\ u [0-9a-f]{4} )* \\" )
(?<array> \\[ (?: (?&json) (?: , (?&json) )* )? \\s* \\] )
(?<pair> \\s* (?&string) \\s* : (?&json) )
(?<object> \\{ (?: (?&pair) (?: , (?&pair) )* )? \\s* \\} )
(?<fieldname> [a-zA-Z0-9\\_\\-]+ )
(?<json> \\s* (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) )
(?<fieldlist> (?: (?&fieldname) (?: , (?&fieldname) )+ )+ ) )
(?<actiontag> (?&tagnames)
)
(?:\\=
(?\'params\'
(?:
(?\'match_list\'(?&fieldlist))
|
(?\'match_json\'(?&json))
|
(?\'match_string\'(?:[[:alnum:]\\_\\-]+))
)
)
)', 'mig')
const str = `@FIELDLIST=field_1,field_2
@STRING="Bar"
@JSON={"foo":"bar"}
@CUSTOM-AT-2={"strict":true, "targets":["field_3", "field_7"], "title":"Custom Title", "message":"This is a custom message"}
@CUSTOM-AT-1={"strict":true, "targets":["field_3", "field_7"], "title":"Custom Title", "message":"This is a custom message"}`;
// 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