下文展示了几种javascript正则表示式的repalce方式,有些方式我们很少在别的地方看到,如第二种和第三方中方法。
//下面的例子用来获取url的两个参数,并返回urlRewrite之前的真实Urlvar reg = new RegExp( "(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx" , "gmi");
var url = "http://www.qidian.com/BookReader/1017141,20361055.aspx" ;
//方式一,最简单常用的方式
var rep =url.replace(reg , "$1ShowBook.aspx?bookId=$2&chapterId=$3");
alert(rep);
//方式二 ,采用固定参数的回调函数
var rep2 =url.replace(reg , function(m ,p1 ,p2 ,p3){ return p1 + "ShowBook.aspx?bookId=" +p3 + "&chapterId=" +p3});
alert(rep2);
//方式三,采用非固定参数的回调函数
var rep3 =url.replace(reg , function(){ var args =arguments ; return args[ 1] + "ShowBook.aspx?bookId=" +args[ 2] + "&chapterId=" +args[ 3];});
alert(rep3);
//方法四
//方式四和方法三很类似, 除了返回替换后的字符串外,还可以单独获取参数
var bookId ;
var chapterId ;
function capText()
{
var args =arguments ;
bookId =args[ 2];
chapterId =args[ 3];
return args[ 1] + "ShowBook.aspx?bookId=" +args[ 2] + "&chapterId=" +args[ 3];
}
var rep4 =url.replace(reg ,capText);
alert(rep4);
alert(bookId);
alert(chapterId);
//除了使用replace方法获取正则表示式的分组外,还可以使用test ,exec方法获取分组,只是手法有所不同而已
var reg2 = new RegExp( "(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx" , "gmi");
var m =reg2.exec( "http://www.qidian.com/BookReader/1017141,20361055.aspx");
var s = "" ;
//获取所有的分组
for (i = 0 ; i < m.length ; i ++) {
s = s + m[i] + "\n" ;
}
alert(s);
bookId =m[ 2];
chapterId =m[ 3];
alert(bookId);
alert(chapterId);
//使用test方法获取分组
var reg3 = new RegExp( "(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx" , "gmi");
reg3.test( "http://www.qidian.com/BookReader/1017141,20361055.aspx");
//获取三个分组
alert( RegExp.$1);
alert( RegExp.$2);
alert( RegExp.$3);
var str="www.baidu.com";
//str.format("好","q")str.replace(new RegExp("(\\.)(bai)du","g"),function(){
for(var i=0;i<arguments.length;i++)
{
document.write(arguments[i]+"<br/>");
}
document.write("-------------------------------------------------<br/>");
});
两个例子(证明,replace传入正则参数和字符传参数结果不同):
alert("123".replace("1",function(){var un;return un;})); //弹出undefined23
alert("123".replace(new RegExp("1"),function(){var un;return un;})); //弹出23
第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。
replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\./, "!" ); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\./g, "!" ); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”
所以可以用以下几种方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);
string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。
- <script type="text/javascript">
- String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
- if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
- return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
- } else {
- return this.replace(reallyDo, replaceWith);
- }
- }
- </script>