javascript的字符串替换

参数说明
findstring 目标字符串
Required. Specifies a string value to find. To perform a global search add a 'g' flag to this parameter and to perform a case-insensitive search add an 'i' flag
必选项。指定所要替换的字符串。要执行多次匹配需要添加一个”g“标记。要指定模糊匹配需要添加一个”i“标记

newstring 新字符串
Required. Specifies the string to replace the found value from findstring
必选项。指定所要替换的字符串的新值

全部替换可参考如下实现:

1
var testStr = "aaabbbcccdddaaa";
alert(testStr.replace(/a/g,'x'));
将字符串testStr中的所有a全部替换成x

2
stringObj.replace(new RegExp(oldString,"gm"),newString))
说明:
gm的g表示global, m表示multiLine,可以实现替换全部指定字串。

3
var testStr = "aaabbbcccdddaaa";
str=str.split("a").join("x");
alert(str) ;
将字符串testStr中的所有a多替换成x

参考文档:http://wangyi878750.blog.sohu.com/31370032.html

posted @ 2011-08-24 10:03  红油小生  Views(198)  Comments(0Edit  收藏  举报