Javascript String format(arg)

Description

Javascript String format(arg)

String.prototype.format = function(arg) {
  var argLen, args, repFn, str;
  repFn = void 0;/* www  .j  a  v  a 2  s .c  om*/
  str = this;
  if (typeof arg === "object") {
    repFn = function(a, b) {
      if (arg[b]) {
        return arg[b];
      } else {
        return a;
      }
    };
  } else {
    args = arguments;
    argLen = args.length - 1;
    str = str.replace(/(?!\{)*\{\{(\w+)\}\}/g, function(a) {
      var ret;
      args[++argLen] = a.replace("{{", "{").replace("}}", "}");
      ret = "{" + argLen + "}";
      return ret;
    });
    repFn = function(a, b) {
      return args[parseInt(b, 10)];
    };
  }
  return str.replace(/(?!\{)*\{(\w+)\}/g, repFn);
};



PreviousNext

Related