javascriptregextabulator

JS reRegExp Expression for guitar chords


I'm on the way to write a tabulator-Editor for my music project and need to format the chordnames. Everything works fine except special bass- oder high-notes f.e. Am7/bb\c#. In that example it should look like Am7/Bb\C# and the chars behind / or \ should be <sup> in html.

So my question is: How can I write an expression with replace /bb, \c# or only /a, \f? It's not necessary that every chord has a/* or a* (bass- or highnote) Thanks a lot Detlef

At the moment I format my Chords like this:

function formatChords(obj, value) {         
  if(!value) return;         
  var newValue = value;
  var formated = newValue.replaceAll('#', '<sup class="superSS">#</sup>');
  formated = formated.replaceAll('b', 'B'); 
  formated = formated.replaceAll('bb', 'B<sup class="superSS">b</sup>');
  formated = formated.replaceAll('BB', 'B<sup class="superSS">b</sup>');
  formated = formated.replace(new RegExp("\\d", "g"), '<sup class="superSS">$&</sup>');
  formated = formated.charAt(0).toUpperCase() + formated.slice(1);
  obj.html(formated);     
}

Solution

  • Use replace with a regular expression to match the patters and replace them with the desired format:

    function formatChords(obj, value) {         
      if(!value) return;         
      var newValue = value;
      var formated = newValue.replaceAll('#', '<sup class="superSS">#</sup>');
      formated = formated.replaceAll('b', 'B'); 
      formated = formated.replaceAll('bb', 'B<sup class="superSS">b</sup>');
      formated = formated.replaceAll('BB', 'B<sup class="superSS">b</sup>');
      formated = formated.replace(new RegExp("\\d", "g"), '<sup class="superSS">$&</sup>');
      formated = formated.charAt(0).toUpperCase() + formated.slice(1);
    
      formated = formated.replace(/\/([a-gA-G]#?b?)/g, '/<sup class="superSS">$1</sup>');
      formated = formated.replace(/\\([a-gA-G]#?b?)/g, '\\<sup class="superSS">$1</sup>');
    
      obj.html(formated);     
    }
    

    The expressions should match a slash or a backslash followed by a note.