Javascript String substitute()

Description

Javascript String substitute()


/**(c) Copyright 2015 Zh. All Rights Reserved. 2015-11-15**/
String.prototype.substitute = function(){
 if (!this) return '';
 var args=[];/*  ww  w  .  j av  a  2  s .  c  om*/
 if(arguments.length==1&&Array.isArray(arguments[0]))
  args = arguments[0];
 else
  args = arguments;
 var len = args.length;
 var str = this;
 for (var i = 0; i < len; i++)
 {
  str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
 }
 return str;
};



PreviousNext

Related