javascript中使用replaceAll()函数实现字符替换的方法
2010-12-25 00:00
941 查看
而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:用于替换的子字符串。
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>
相关文章推荐
- javascript中使用replaceAll()函数实现字符替换的方法
- javascript中使用replaceAll()函数实现字符替换的方法
- 对字符串使用replaceAll()方法替换 * ? + / | 等字符的时候会报以下异常
- Javascript中使用replace()方法+正则表达式替换掉所有字符
- javascript中使用splice方法轻松实现对数组元素的插入、删除、替换
- JSP中有关时间和日期类的使用与JSP中的字符替换函数 str_replace()实现
- JavaScript 截取字符函数的使用方法
- JavaScript基于扩展String实现替换字符串中index处字符的方法
- (转)JavaScript escape() 函数(该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。)
- [Javascript] String.replace 正则表达式及替换函数使用 $ 字符
- JavaScript使用setInterval()函数实现简单轮询操作的方法
- JavaScript中字符替换函数String.replace()使用技巧
- [Effective JavaScript 笔记]第26条:使用bind方法实现函数的柯里化
- JavaScript实现替换字符串中最后一个字符的方法
- JavaScript使用setInterval()函数实现简单轮询操作的方法
- JavaScript的String类型replace()方法介绍和使用replace()方法实现简单html模板替换功能
- 字符串替换空格: 请实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we%20are%20happy.”。
- python join字符连接函数的使用方法
- AJAXPRC实现了使用Javascript调用服务端方法
- javascript中function的call()函数的使用-----替换当前函数中的当前对象