面试时遇到的题目。正则,replace()

function Fn(str){
  this.str = str;
} 
Fn.prototype.format = function(){
  var arg = arguments;
  var dd =  this.str.replace(/{[0-9]}/g,function(a,b){
    var _index = a.replace(/\{|}/g,'');
    console.log(_index);
    return arg[_index]||''
  })
  return dd;
}

var fn = new Fn('<p><a href="{0}">{1}</a></p>')
document.write (fn.format('http://www.baidu.com/','百度'));

 

posted on 2019-04-12 18:58  _jackie  阅读(174)  评论(0编辑  收藏  举报

导航