String formatting JS-

String.prototype.format=function(){
  var content = this;
  for(var i = 0; i < arguments.length; i++){
    var reg = new RegExp("\\{" + i + "\\}", "g");
    content = content.replace(reg, arguments[i]);
  }
  return content;
}
var s="my name is {0}, I'm {1} years old, and I have {2} brother.";
s=s.format("wendy",24,2);
console.log(s);

prototype: string used to add properties and methods

arguments: all functions of local variables, not Array, only the length, index

RegExp: 'g' (global match ), 'i' ( case-insensitive matching ), 'm' ( multi-line matching )

Published 21 original articles · won praise 0 · Views 600

Guess you like

Origin blog.csdn.net/Stodger0216/article/details/104059431