第一次发现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替换字符串中所有指定的字符的更多相关文章

  1. js 替换字符串中所有匹配的字符

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...

  2. js替换字符串中的数字或非数字

    替换字符串中的数字 var text = "abc123"; text=text.replace(/[0-9]/ig,""); 此时得到的text为" ...

  3. JS判断字符串中是否存在某个字符

    用String类中的indexOf函数,例如:String str="we find out sth";if(str.indexOf("o")==-1){ // ...

  4. js判断字符串是否包含指定的字符

    判断字符串是否包含指定字符是很常用的功能,比如说,注册时用户名限制不能输入"管理员",或者需要js判断url跳转链接是否包含某个关键词等-- <!DOCTYPE html&g ...

  5. js 替换字符串中的双引号

    text.replace(/\"/g, ''); 可根据此方法去掉字符串中的双引号

  6. js replace(a,b)之替换字符串中所有指定字符的方法

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...

  7. 如何用原生js替换字符串中的某个字符(或字符串)为指定的字符串?

    <html> <head><title>我的第一个 HTML 页面</title></head><script type=" ...

  8. js替换字符串中全部“-”

    alert("2014-03-22".replace('-','')); alert("2014-03-22".replace(/-/g,'')); 第一个运行 ...

  9. [JS]计算字符串中出现最多的字符和其出现次数

    这是一道面试题 此处是利用Obj来解决的,当然不只此一种方法. //思路:遍历数组,拿到一个字符,并将之以 "字符":出现次数 的key:value形式存到对象中. //如果此字符 ...

随机推荐

  1. CentOS编译安装emacs并配置

    Liunxs中CentOS系列一向以稳定为目标,然而也会存在版本太旧的问题,emacs就是其中的一个,目前emacs都发行到25.2了,而CentOS上的emacs版本却还是23.1.所以需要下载源代 ...

  2. 谷歌广告Admob在cocos2dx上通过回调实现底部Banner

    首先说明我的开发平台,以免由于平台问题造成不必要的误解: cocos2dx-3.4 ndk-r9d eclipse Admob是谷歌官方广告,已经集成在google_play_service_lib包 ...

  3. python3 selenium 如何处理异常情况

    使用场景: 1.元素只有在某一特定情况下才会出现 2.元素定位不到 使用方法: try: except exceptions.NoSuchElementException: 举例说明: # _._ c ...

  4. ③jQuery生成html元素

    动态生成HTML元素 绑定事件

  5. AngularJs 常用的过滤器

    date格式化 {{ 1304375948024 | date }}             //结果:May 3, 2011 {{ 1304375948024 | date:"MM/dd/ ...

  6. 学习笔记:javascript内置对象:数组对象

    1.数组对象的创建   1.设置一个长度为0的数组  var myarr=new array(); 2.设置一个长度为n的数组  var myarr=new arr(n); 3.声明一个赋值的指定长度 ...

  7. [刷题]算法竞赛入门经典(第2版) 4-7/UVa509 - RAID!

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa509 - RAID! #include<iostream> ...

  8. Cygwin Unable to get setup from *

    Cygwin Unable to get setup from * 错误 解决方案 是因为用自定义镜像站点,比如 http://mirrors.xdlinux.info/cygwin/x86_64/ ...

  9. spring-定时器(2)

    Spring提供的三种定时任务机制及其比较 定时任务的需求在众多应用系统中广泛存在,在Spring中,我们可以使用三种不同的定时机制,下面一一描述并加以比较 1. 基于Quartz的定时机制 下面详细 ...

  10. 如何通过 WebP 兼容减少图片资源大小

    作者:学军又拍云 CDN 服务公测 WebP 自适应功能,为客户减少图片资源大小.本文我们将一起来阐述WebP兼容的来龙去脉. 前言我们知道,理想的网页应该在 1 秒内打开,而在页面的整体大小中,图片 ...