cycle through regex

by toubia95

HTML

<div id="display"></div>

CSS

* {
  font-family: Monaco, 'Andale Mono', 'Lucida Console', 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;
  font-size: 11.9px;
  line-height: 17.85px;
}

JavaScript

// Performance.now() polyfill (aka perf.now())
// Paul Irish, 2015
// https://gist.github.com/paulirish/5438650
(function () {
  if ("performance" in window == false) {
    window.performance = {};
  }
  Date.now = (Date.now || function () { // thanks IE8
    return new Date().getTime();
  });
  if ("now" in window.performance == false) {
    var nowOffset = Date.now();
    if (performance.timing && performance.timing.navigationStart) {
      nowOffset = performance.timing.navigationStart
    }
    window.performance.now = function now() {
      return Date.now() - nowOffset;
    }
  }
})();

// Calcul de la vitesse d'execution de scripts
// Gilles Toubiana 16/11/2015
function speed() {
  'use strict';
  var result = "",
    i, j, p1, p2, func, duree = 1000,
    len = arguments.length;
  for (i = 0; i < len; i += 1) {
    p1 = performance.now();
    func = arguments[i];
    for (j = 0; j < duree; j += 1) {
      func();
    }
    p2 = Math.round((performance.now() - p1) * 1000) / 1000;
    result += arguments[i].toString() + " -> " + p2 + "μs\n";
  }
  result = result.replace(/function\s?\(\)\s?{([^}]*)}\s->/g, '$1 //');
  alert(result);
}

// CONVERSIONS MULTIPLES ***************************************

var str = "%Jm/%Mb/%A/%UHt";
display.innerHTML += str + "<br>";

str = str.replace(/%[JMAmb]+/g, function(result) {
    var min,maj;
    if (result.match(/m/)) { maj = "m" } ;
    if (result.match(/b/)) { min = "b" } ;
    if (result.match(/J/)) { result = "Jour"; };
    if (result.match(/M/)) { result = "Mois"; };
    if (result.match(/A/)) { result = "Année"; };
    if (maj) { result = result.toUpperCase(); } ;
    if (min) { result = result.toLowerCase(); } ;
    return result;
});
display.innerHTML += str + "<br>";

// CONVERSIONS MULTIPLES ***************************************


/** test speed */
/*speed(function () {func(2015);},
      function () {func(2016);}
);
*/