const regex = /\b(?:G,C,D|A,B,C|E,C,D)|(?:[ABCDEFG](?:#|b)?)(?:\/[ABCDEFG]b)?(?:(?:(?:maj|min|sus|add|aug|dim)(?:\d{0,2}(?:#\d{1,2}|sus\d)?)?)|(?:m\d{0,2}(?:(?:maj|add|#)\d{0,2})?)|(?:-?\d{0,2}(?:\([^)]*\)|#\d{1,2})?))?/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(?:G,C,D|A,B,C|E,C,D)|(?:[ABCDEFG](?:#|b)?)(?:\\\/[ABCDEFG]b)?(?:(?:(?:maj|min|sus|add|aug|dim)(?:\\d{0,2}(?:#\\d{1,2}|sus\\d)?)?)|(?:m\\d{0,2}(?:(?:maj|add|#)\\d{0,2})?)|(?:-?\\d{0,2}(?:\\([^)]*\\)|#\\d{1,2})?))?', 'g')
const str = `<p>Intro:G,C,D D, A, Em, Eb Ebb G G,C,D</p>
A,B,C <p><br />
Verse 1:</p> <p> D A Em <br />
Yeah I've iced down a few thousand beers in my days<br />
G <br />
You put down twice as many<br /> D A Em <br />
C9 G C#/Db C# Dsus2sus4
Dmaj13#11
Bmaj13#11
A7(b5,#9)
A7#9
B-5
#sus #maj #min #aug`;
// 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