1. 程式人生 > >面試時遇到的題目。正則,replace()

面試時遇到的題目。正則,replace()

color prot nbsp spa pan code var nts ref

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/‘,‘百度‘));

面試時遇到的題目。正則,replace()