const regex = /(^\s+(?P<att_name>[^\s]+)\s+(?P<att_value>[^\s]+)(?P<att_body>\s{[\s\S]+?^\s})?)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(^\\s+(?P<att_name>[^\\s]+)\\s+(?P<att_value>[^\\s]+)(?P<att_body>\\s{[\\s\\S]+?^\\s})?)$', 'gm')
const str = `{
enabled 1
TARGET iqn.2012-05.local:mailbox.target.mccip001{
rel_tgt_id 1
IncomingUser "user1234 secret123456"
enabled 0
MaxSessions 4
}
TARGET iqn.2012-05.local:mailbox.target.mccip002 {
rel_tgt_id 2
IncomingUser "user1234 secret123456"
GROUP access_group{
LUN 0 dev2-0
LUN 1 dev2-1
LUN 2 dev2-2
LUN 3 dev2-3
LUN 4 dev2-4
LUN 5 dev2-5
LUN 6 dev2-6
LUN 7 dev2-7
LUN 8 dev2-8
LUN 9 dev2-9
INITIATOR iqn.1994-09.org.freebsd:*
}
enabled 1
MaxSessions 10
}
TARGET iqn.2012-05.local:mailbox.target.mccip003 {
rel_tgt_id 3
IncomingUser "user1234 secret123456"
GROUP access_group {
LUN 0 dev3-0
LUN 1 dev3-1
INITIATOR iqn.1994-09.org.freebsd:*
}
enabled 1
MaxSessions 2
}
}`;
// 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