const regex = /^@!?([A-Za-z0-9_-]+) *\( *((.+) *)?\) *?\n((\s+).*\n(((\5).*\n)|\n)*)?/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^@!?([A-Za-z0-9_-]+) *\\( *((.+) *)?\\) *?\\n((\\s+).*\\n(((\\5).*\\n)|\\n)*)?', 'gmi')
const str = `### Labels
@label(blue, Free)
@label(green, New)
@label(orange, OMG)
@label(grey, boring)
@label(pink, Much colour)
@label(yellow, Many label)
@label(red, wow)
@label(green, New, after)
Check out our new documentation
@label(blue, Link, after, https://moltin.com)
Ut gravida turpis vitae velit porta elementum.
Ut a placerat neque. Curabitur sed est vestibulum, vestibulum dui et, fringilla purus. Vestibulum at ligula eget libero fringilla fringilla. Aenean vitae lobortis dui, non accumsan est. Donec in justo vel dolor [tristique](/) auctor quis id nunc. Quisque felis ex, interdum sit amet dui tincidunt, euismod molestie tellus. Suspendisse accumsan, ipsum ac interdum mollis.`;
// 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