Javascript String parseManaCost()

Description

Javascript String parseManaCost()


// http://www.codewars.com/kata/regexp-basics-parsing-mana-cost

String.prototype.parseManaCost = function() {
  if (/[^\dwubrg]/i.test(this.valueOf())) return null;
  
  const types = "wubrg";
  
  let s = this.valueOf();
  let mana = {};//  w w  w.ja v a 2  s.c o m
  let m;
  
  m = s.match(/\d+/);
  if (m && m[0] != 0) mana['*'] = +m[0];
  
  s = s.split('').sort().join('');
  for (let t of types) {
    t = t.toLowerCase();
    m = s.match(new RegExp(`${t}+`, 'i'));
    if (m) mana[t] = m[0].length;
  }
  
  return mana;
}



PreviousNext

Related