const regex = /(?(DEFINE)
(?<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'
\@(?&fieldname)
)
(?:\=
(?'params'
(?:
(?'match_list'(?&fieldlist))
|
(?'match_json'(?&json))
|
(?'match_string'(?:[[:alnum:]_-]+))
)
)
)?/igm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?(DEFINE)
(?<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\'
\\@(?&fieldname)
)
(?:\\=
(?\'params\'
(?:
(?\'match_list\'(?&fieldlist))
|
(?\'match_json\'(?&json))
|
(?\'match_string\'(?:[[:alnum:]_-]+))
)
)
)?', 'igm')
const str = `@tag=1,2,3
@b=asdferf
@tag=a,1,b,foo_123,asf
@tag=a,b,c @A_S:-DF=foo
@ABC={"f":{"A":"b"}}
@A=[
{"a":"b"},
{"C":"D"}
] @B
@A={"a":"b"}
@ASDF={
"foo":"bar",
"fdsa":"fdsa"
}
@ASDF=FOO
FOO=ASD @ASDFFADS={"ASDF":"fdsa"}
@ASDF{"asdf":"asdf"}
@ASDF="This is a place holder" @FDSA="James Brown" @D=Fing poo
@FEE @BOO @IMAGE_TAG=FOO @IMAGE-tag=ASDF2
@ASDF={"foo":"bar"} FOO=ASDF @ASDFFADS={"ASDF":"fdsa"}
@t3={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}`;
// 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