Created
October 14, 2019 11:59
-
-
Save hyvyys/a8601e11af1ba145595f82896393d6f1 to your computer and use it in GitHub Desktop.
Regular expression to match Chord symbol / name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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