const regex = / # begin capture group 1
\{\s+"name":[ ]" # match '{' then 1+ whitespace chars, then '"name": "
(\w+) # match 1+ word chars, save to capture group 1
- # match '-'
(\w+) # match 1+ word chars, save to capture group 2
- # match '-'
(\w+) # match 1+ word chars, save to capture group 3
- # match '-'
(\w+) # match 1+ word chars, save to capture group 4
( # begin capture group 5
",\s+"units":[ ] # match '",', then 1+ whitespaces then '"units": '
"\w+" # match 1+ word chars
,\s+"value":[ ] # match ',', then 1+ whitespaces then '"value": '
\d+ # match 1+ digits
\s+\} # match 1+ whitespaces then '"units": '
) # end capture group 5
# end capture group 1
(?! # begin negative lookahead
.* # match 0+ chars
\{\s+"name":[ ]" # match '{' then 1+ whitespace chars then '"name": ' then '"'
\1_ # match contents of capture group 1 then '_'
\2_ # match contents of capture group 2 then '_'
\3_ # match contents of capture group 3 then '_'
\4 # match contents of capture group 4
\5 # match contents of capture group 5
) # end of negative lookahead
| # or
\{\s+"name":[ ]" # match '{' then 1+ whitespace chars, then '"name": "
\w+ # match 1+ word chars
_ # match '_'
\w+ # match 1+ word chars
_ # match '_'
\w+ # match 1+ word chars
_ # match '_'
\w+ # match 1+ word chars
(?5) # execute code constructing capture group 6
/gms;
// Alternative syntax using RegExp constructor
// const regex = new RegExp(' # begin capture group 1
\\{\\s+"name":[ ]" # match \'{\' then 1+ whitespace chars, then \'"name": "
(\\w+) # match 1+ word chars, save to capture group 1
- # match \'-\'
(\\w+) # match 1+ word chars, save to capture group 2
- # match \'-\'
(\\w+) # match 1+ word chars, save to capture group 3
- # match \'-\'
(\\w+) # match 1+ word chars, save to capture group 4
( # begin capture group 5
",\\s+"units":[ ] # match \'",\', then 1+ whitespaces then \'"units": \'
"\\w+" # match 1+ word chars
,\\s+"value":[ ] # match \',\', then 1+ whitespaces then \'"value": \'
\\d+ # match 1+ digits
\\s+\\} # match 1+ whitespaces then \'"units": \'
) # end capture group 5
# end capture group 1
(?! # begin negative lookahead
.* # match 0+ chars
\\{\\s+"name":[ ]" # match \'{\' then 1+ whitespace chars then \'"name": \' then \'"\'
\\1_ # match contents of capture group 1 then \'_\'
\\2_ # match contents of capture group 2 then \'_\'
\\3_ # match contents of capture group 3 then \'_\'
\\4 # match contents of capture group 4
\\5 # match contents of capture group 5
) # end of negative lookahead
| # or
\\{\\s+"name":[ ]" # match \'{\' then 1+ whitespace chars, then \'"name": "
\\w+ # match 1+ word chars
_ # match \'_\'
\\w+ # match 1+ word chars
_ # match \'_\'
\\w+ # match 1+ word chars
_ # match \'_\'
\\w+ # match 1+ word chars
(?5) # execute code constructing capture group 6
', 'gms')
const str = `{
"body": {
"metricsArray": [
{
"name": "free-aa-bb2-123x123Profiles",
"units": "profiles",
"value": 14
},
{
"name": "free_aa_bb2_123x123Profiles",
"units": "profiles",
"value": 14
}
],
"name": "regionxxx",
"timeStamp": "2022-01-20T04:58:29.875Z"
}
}
`;
// 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