String.prototype.format=function(){ if(arguments.length===0){ return String(this); } let reg=/(\{\d\})/; let r=this; while(reg.test(r)){ let index=RegExp.$1.slice(1,-1); r = r.replace(new RegExp("\\{"+index+"\\}","g"),(index >= arguments.length)?"":arguments[index]); } return r; };
Javascript string replace the placeholder
"" Hello, I'm {0}, {2} years old this year, gender {1} end of the test ".format ( 'Devil', 'M', '18 ' )
" Hello, I am the devil, this year 18 years old, male gender, the end of the test