首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-09-19 
js 实现动态替换 占位符 和取contextvar MessageFormat function(str){??? var args [].splice.call(a

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])

热点排行
Bad Request.