const regex = /((?<stat>\w+)(\s*([Dd][cC]|@)\s*(?<num>\d+))?|())\s*\]/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?<stat>\\w+)(\\s*([Dd][cC]|@)\\s*(?<num>\\d+))?|())\\s*\\]', 'gm')
const str = `[1 min: STR-Armor Rating](swept downstream to [room L]/able to reach edge)
[1min: STR DC 12](swept downstream to [room L]/able to reach edge)
[12 hr: STR DC 12](swept downstream to [room L]/able to reach edge)
How does one [WIS@15](fail info/pass info 1)
[ssdsd](sd/ds)
[ssdsd](-/ds)
[ssdsd](player is Incapacitated for 1d4 rounds/-)
[Con](sleep for 1d6x10 minutes/-)
[Dex](2d6 damage/half)
[CON](lose 1hp every minute/-)
[STR](-/upward thrust dislodges debris - critical: door opens)
[DEX](slide down to pit/-)
[level 2 - room T]
[CON-Armor Rating](1d8 damage/-)
[STR-Armor Rating](swept downstream to [room L]/able to reach edge)
[Dex](-/pull up bracelet with chain)
[paralyze](paralyzed 3d6 turns/-)
[wis](cannot understand/-)
[poison](unconscious 1d6 turns/-)`;
// 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