Skip to content

Instantly share code, notes, and snippets.

@hyvyys
Created October 14, 2019 11:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyvyys/a8601e11af1ba145595f82896393d6f1 to your computer and use it in GitHub Desktop.
Save hyvyys/a8601e11af1ba145595f82896393d6f1 to your computer and use it in GitHub Desktop.
Regular expression to match Chord symbol / name
function chordRegex() {
const note = '[A-G][b#]?';
const altered = `(?:5|dim(5|7)?|aug5?|\\+5?|-5?)`;
const minor = '(?:mi?n?)';
const major = '(?:maj?|Ma?j?)';
const majorableExt = `(?:6|7|9|11|13)`;
const ext = `(?:4|6|7|9|11|13|6\\/9)`;
const _mod = '(?:[b-](5|6|9|13)|[#+](4|5|9|11))';
const mod = `(?:\\(${_mod}\\)|${_mod})`
const sus = '(?:sus(2|4|24|2sus4)?)';
const add = '(?:add[b#]?(?:2|4|6|7|9|11|13))';
const bass = `(?:\\/${note})`;
const lookahead = '(?=$| )';
const source = `${note}${
`(?:${altered}|${
`(?:${minor}?(?:${ext}|${major}?${majorableExt})?)`
+ `${mod}*${sus}?${mod}*${add}?`
})`
}${bass}?${lookahead}`;
console.log(JSON.stringify(source));
return source;
}
// result:
const source = "[A-G][b#]?(?:(?:5|dim(5|7)?|aug5?|\\+5?|-5?)|(?:(?:mi?n?)?(?:(?:4|6|7|9|11|13|6\\/9)|(?:maj?|Ma?j?)?(?:6|7|9|11|13))?)(?:\\((?:[b-](5|6|9|13)|[#+](4|5|9|11))\\)|(?:[b-](5|6|9|13)|[#+](4|5|9|11)))*(?:sus(2|4|24|2sus4)?)?(?:\\((?:[b-](5|6|9|13)|[#+](4|5|9|11))\\)|(?:[b-](5|6|9|13)|[#+](4|5|9|11)))*(?:add[b#]?(?:2|4|6|7|9|11|13))?)(?:\\/[A-G][b#]?)?(?=$| )";
const regex = chord: new RegExp(chordRegex());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment