I have a song lyrics and I'm using a function to highlight the parts of the lyrics. The code looks like this:
var span = {
intro : '<span style="color: Magenta ;">',
verse : '<span style="color: DodgerBlue;">',
prechorus : '<span style="color: DeepPink;">',
chorus : '<span style="color: Tomato;">',
bridge : '<span style="color: LimeGreen;">',
outro : '<span style="color: Magenta ;">',
repeat : '<span style="color: Silver;" title="Repeat">',
close : '</span>',
};
function highlight(lyrics) {
var highlighted = lyrics.replace(/\[.*\]/g, function(match) {
switch(match) {
case "[Intro]": return span.io + match + span.close; break;
case "[Verse 1]": return span.verse + match + span.close; break;
case "[Verse 2]": return span.verse + match + span.close; break;
case "[Verse 3]": return span.verse + match + span.close; break;
case "[Verse 4]": return span.verse + match + span.close; break;
case "[Verse 5]": return span.verse + match + span.close; break;
case "[Pre-Chorus 1]": return span.prechorus + match + span.close; break;
case "[Pre-Chorus 2]": return span.prechorus + match + span.close; break;
case "[Pre-Chorus 3]": return span.prechorus + match + span.close; break;
case "[Pre-Chorus 4]": return span.prechorus + match + span.close; break;
case "[Chorus 1]": return span.chorus + match + span.close; break;
case "[Chorus 2]": return span.chorus + match + span.close; break;
case "[Chorus 3]": return span.chorus + match + span.close; break;
case "[Chorus 4]": return span.chorus + match + span.close; break;
case "[Chorus 5]": return span.chorus + match + span.close; break;
case "[Bridge 1]": return span.bridge + match + span.close; break;
case "[Bridge 2]": return span.bridge + match + span.close; break;
case "[Outro]": return span.io + match + span.close; break;
}
});
return highlighted.replace(/\(R.{0,3}\)/g, span.repeat + "$&" + span.close);
}
highlight
function expects a text. First, it finds the strings (headers of lyrics parts) that are enclosed by brackets, then using switch, it checks the found matches and wraps them in a corresponding span tag.
My problem is that I don't want to use five different cases to replace verses, or any other repeating headers. Instead I want to use a regexp. Something like:
case /\[Verse.*?\]/g: return span.verse + match + span.close; break;