发表于: sitebuild | 作者: | 日期: 2008/11/11 09:11

stringObject.replace(findstring,newstring)

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.

参数说明
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

: https://blog.darkmi.com/2008/11/11/427.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.