和音を含む行を一致させようとしていますが、呼び出し元に返されたくないため、文字を消費せずに各一致が空白で囲まれているか、行の最初にあることを確認する必要があります。
例えば
Standard Tuning (Capo on fifth fret)
Time signature: 12/8
Tempo: 1.5 * Quarter note = 68 BPM
Intro: G Em7 G Em7
G Em7
I heard there was a secret chord
G Em7
That David played and it pleased the lord
C D G/B D
But you don't really care for music, do you?
G/B C D
Well it goes like this the fourth, the fifth
Em7 C
The minor fall and the major lift
D B7/D# Em
The baffled king composing hallelujah
Chorus:
G/A G/B C Em C G/B D/A G
Hal - le- lujah, hallelujah, hallelujah, hallelu-u-u-u-jah ....
「68 BPM」の「B」にも一致することを除いて、ほとんど機能します。コードが正しく一致していることを確認するにはどうすればよいですか? Before の B や SUBSIDE の D や E と一致させたくないのですが?
これは、個別の行ごとに一致させるための私のアルゴリズムです。
function getChordMatches(line) {
var pattern = /[ABCDEFG](?:#|##|b|bb)?(?:min|m)?(?:maj|add|sus|aug|dim)?[0-9]*(?:\/[ABCDEFG](?:#|##|b|bb)?)?/g;
var chords = line.match(pattern);
var positions = [];
while ((match = pattern.exec(line)) != null) {
positions.push(match.index);
}
return {
"chords":chords,
"positions":positions
};
}
つまり、["A"、"Bm"、"C#"] ではなく ["A"、"Bm"、"C#"] の形式の配列が必要です。
編集
受け入れられた回答を使用して機能させました。先頭の空白に対応するために、いくつかの調整を行う必要がありました。時間を割いてくれてありがとう!
function getChordMatches(line) {
var pattern = /(?:^|\s)[A-G](?:##?|bb?)?(?:min|m)?(?:maj|add|sus|aug|dim)?[0-9]*(?:\/[A-G](?:##?|bb?)?)?(?!\S)/g;
var chords = line.match(pattern);
var chordLength = -1;
var positions = [];
while ((match = pattern.exec(line)) != null) {
positions.push(match.index);
}
for (var i = 0; chords && i < chords.length; i++) {
chordLength = chords[i].length;
chords[i] = chords[i].trim();
positions[i] -= chords[i].length - chordLength;
}
return {
"chords":chords,
"positions":positions
};
}