日期:2014-05-16  浏览次数:20302 次

js 实现动态替换 占位符 和取context

var MessageFormat = function(str){
??? var args = [].splice.call(arguments,1);

//因为aruguments是个伪数组,没有数组的splice方法,所以,我们要用call给arguments只想一个aplice的方法,这样我们//就可以截取到 arguments后面的参数了
??? return str.replace(/\{([^}])\}/g,function(index,value){
??????? return args[value];

//下面我们有那个了replace,利用正则将我们捕获到的占位符,替换掉
??? })
}

?

//取url中的context

var str ="http://my.tianji.com/dashboard/777";
aa = str.match(/https?:\/\/[^/]+\/([^/]+)/);
console.log(aa[1])