这有一个很简单的交换两个单词顺序的例子,如果不用repalce可以这么做: 复制代码 代码如下: (function(){ var str = "click dblclick"; var result = str.split(//).reverse().join("") console.log(result); })()
这么做略显麻烦,用replace处理则显得清爽许多: 复制代码 代码如下: (function(){ var str = "click dblclick"; var result = str.replace(/(w+)(s+)(w+)/,"$3$2$1"); console.log(result); })();
再看另一个例子——给数字加上千分位: 复制代码 代码如下: (function(){ var number = 12345678900; var result = (""+number).replace(/(d)(?=(d{3})+$)/g,"$1,"); console.log(result); })();
不过现在要扯的是replacement作为函数的用法,所以多此一举的把上面的形式改成函数的形式: 复制代码 代码如下: (function(){ var str = "click dblclick"; var result = str.replace(/(w+)(s+)(w+)/,function(all,$1,$2,$3){ return $3+$2+$1; }); console.log(result); })();
(function(){ var number = 12345678900; var result = (""+number).replace(/(d)(?=(d{3})+$)/g,function(all,$1){ return $1 + ","; }); console.log(result); })();
所以replace无非是捕获匹配的项然后经过处理,作为返回值进行替换,只不过是函数的比较给力。 下面看一个比较实用的例子,大多数语言都有的格式化输出,比如C语言的printf: 复制代码 代码如下: (function(){ var str = "%s may have gone, but there is a time of %s"; var result = str.replace(/(%s)/g,function(){ return "replacement"; }) console.log(result); })()
这里有个问题就是,%s都是一样的,替换出来都是一样的,而且我们只能按照顺序来判断被替换的是哪一部分,如果添加一段,那么后面所有的都得变。所以我们得传个变量进去。 复制代码 代码如下: (function(){ var array = ["Swallows","return"]; var i = 0; var str = "%s may have gone, but there is a time of %s"; var result = str.replace(/(%s)/g,function(){ return array[i++]; }) console.log(result); })(); (function(){ var str = "#{what} may have gone, but there is a time of #{how}"; var obj = { what : "Swallows", how : "return" } var result = str.replace(/(?:#{(w+)})/g,function(all,$1){ return obj[$1]; }) console.log( result); })();
伪装成函数的样子就是: 复制代码 代码如下: (function(){ function gsub(str,replaceObj){ return str.replace(/(?:#{(w+)})/g,function(all,$1){ return replaceObj[$1]; }) } console.log("gsub结果:",gsub("#{what} may have gone, but there is a time of #{how}",{ what : "Swallows", how : "return" })) })();
看第一种方法: 复制代码 代码如下: (function(){ var str = "#{what} may have gone, but there is a time of #{how},\#{reserve}"; function gsub(str,replaceObj){ return str.replace(/(^|.)(?:#{(w+)})/g,function(all,$1,$2){ if($1 == "\"){ return "#{" + $2 +"}"; } return $1 + replaceObj[$2]; }) } console.log("gsub结果:",gsub(str,{ what : "Swallows", how : "return" })) })();
上面需要注意的就是""在字符串中也是转义字符,写的时候也需要转义。
第二种方法:
我们把"#{what}"换成<%what%>的形式。 复制代码 代码如下: (function(){ function gsub(str,replaceObj,regexp){ regexp = regexp || /(?:#{(w+)})/g; return str.replace(regexp,function(all,$1){ return replaceObj[$1]; }) } var str = "<%what%> may have gone, but there is a time of <%how%>,#{reserve}"; console.log("gsub结果:",gsub(str,{ what : "Swallows", how : "return" },/(?:<%(w+)%>)/g)) })();
现在把gsub挂到String的原型上面 复制代码 代码如下: String.prototype.gsub = function(replaceObj,regexp){ regexp = regexp || /(^|.)(?:(#{)(w+)(}))/g; return this.replace(regexp,function(all,$1,$2,$3,$4){ if($1 == "\"){ return $2+$3+$4; } return $1 + replaceObj[$3] ; }) } var str = "<%what%> may have gone, but there is a time of <%how%>,\<%how%>,#{how}"; var obj = { what : "Swallows", how : "return" } console.log("测试1:",str.gsub(obj,/(^|.)(?:(<%)(w+)(%>))/g)); //Swallows may have gone, but there is a time of return,<%how%>,#{how} console.log("测试2:",str.gsub(obj)); //<%what%> may have gone, but there is a time of <%how%>,<%how%>,return