js替换字符串中所有指定的字符
第一次发现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:用于替换的子字符串。
1.<script type="text/javascript">
2.String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
3. if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
4. return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
5. } else {
6. return this.replace(reallyDo, replaceWith);
7. }
8.}
9.</script>
来源:http://fuleonardo.iteye.com/blog/339749
js替换字符串中所有指定的字符的更多相关文章
- js 替换字符串中所有匹配的字符
var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...
- js替换字符串中的数字或非数字
替换字符串中的数字 var text = "abc123"; text=text.replace(/[0-9]/ig,""); 此时得到的text为" ...
- JS判断字符串中是否存在某个字符
用String类中的indexOf函数,例如:String str="we find out sth";if(str.indexOf("o")==-1){ // ...
- js判断字符串是否包含指定的字符
判断字符串是否包含指定字符是很常用的功能,比如说,注册时用户名限制不能输入"管理员",或者需要js判断url跳转链接是否包含某个关键词等-- <!DOCTYPE html&g ...
- js 替换字符串中的双引号
text.replace(/\"/g, ''); 可根据此方法去掉字符串中的双引号
- js replace(a,b)之替换字符串中所有指定字符的方法
var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...
- 如何用原生js替换字符串中的某个字符(或字符串)为指定的字符串?
<html> <head><title>我的第一个 HTML 页面</title></head><script type=" ...
- js替换字符串中全部“-”
alert("2014-03-22".replace('-','')); alert("2014-03-22".replace(/-/g,'')); 第一个运行 ...
- [JS]计算字符串中出现最多的字符和其出现次数
这是一道面试题 此处是利用Obj来解决的,当然不只此一种方法. //思路:遍历数组,拿到一个字符,并将之以 "字符":出现次数 的key:value形式存到对象中. //如果此字符 ...
随机推荐
- C专家编程阅读笔记
周末闲来无事,(哗),好久之前买的C专家编程一直没看,翻起来看了一下 尽量不使用unsigned 尽量不要在代码中使用unsigned,尤其是一些看起来是无符号类型的数字,比如年龄等,因为难免要使用u ...
- oracle修改有数据的字段属性
正常情况下,有数据时不能直接修改属性,我们可以先备份,然后清空现有数据,然后再修改和还原,过程如下: //先缓存表CREATE TABLE T_TABLE1 AS SELECT * FROM tabl ...
- 使用fontawesome图标
我每次找图标时都是在阿里的开源图标库中找的,但是使用起来不是很方便.而我发现了fontawesome之后,觉得实在不错,所以分享给大家. 这是一些参考的文档. fontawesome下载与使用介绍 ...
- (坑)django test在多线程下的问题
问题描述: 使用django自带的test做测试,尝试去数据库中取数据,主线程中没有问题,非主线程中取不到数据. 示例代码: class MyTestCase(TestCase): def setUp ...
- hdu2612 Find a way BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间: 然后对于@的点, ...
- [笔记]ACM笔记 - 自用模板
长期更新. 快速幂 lld pow_mod(lld a, lld b, const int &pr) { lld ans = 1; while (b) { if (b & 1) ans ...
- Linux中grep命令学习
1.简介 grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.Unix的grep家族包括grep.egrep和fgrep.egrep和fgrep的命令只跟grep有很小 ...
- VR全景是市场价值及前景
消费者视角痛点:比如酒店消费行业,很多消费者在预订酒店过程中,都遇到过这样的场景:网上照片里酒店房间看着宽敞明亮,格调不凡,感觉非常喜欢,等真正推开房门插上房卡一看,却大失所望.在酒店行业,网上照片和 ...
- struts2.3.23升级到struts2.3.32
新的漏洞 3月8号去审计厅培训系统的使用,那边计算机中心的负责人递过来一张如下图所示的文档,意思是发现了struts2的漏洞,需要进行修复. 在培训前,我登录到服务器中,看到了项目中,所有的服务器中应 ...
- CoreOS, Kubernetes, etcd
CoreOS CoreOS Container Linux is the leading container operating system, designed to be managed and ...