第一次发现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. [PHP] 网盘搜索引擎-采集爬取百度网盘分享文件实现网盘搜索

    标题起的太大了,都是骗人的.最近使用PHP实现了简单的网盘搜索程序,并且关联了微信公众平台.用户可以通过公众号输入关键字,公众号会返回相应的网盘下载地址.就是这么一个简单的功能,类似很多的网盘搜索类网 ...

  2. python中的一些小知识

    在最近学习python中遇到的一些小问题汇总一下: 1.在windows7下安装python3.5版本时提示安装不了,缺少ServicePack1.  解决办法是,打开控制面板\系统和安全\Windo ...

  3. Telegram学习解析系列(一):认识一下Telegram的源码

    前言: Telegram不知道有多少同行听过这玩意,或者在看它的源码.我是出于工作原因才接触到这东西,看的真是的......变方了!一个月估计刚刚找到门,还没进去多深,把自己的心得和对源码的认识以及我 ...

  4. ubuntu16.04 编译运行 LSD-SLAM

    下载编译LSDSLAM,可能会出现 CreateGlutWindowAndWind is not a member of pangolin 以及 该函数参数报错的问题: 原因是在新的pangolin版 ...

  5. 解析HTML

                                                    解析HTML 一.什么是HTML HTML是超文本标签语言,即网页的源码.而浏览器就是翻译解释HTML源 ...

  6. Linux五种IO模型性能分析

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步:       所谓同步,就是在发出一个功能调用时, ...

  7. ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

    前言 最近公司项目进行架构调整,由原来的三层架构改进升级到微服务架构(准确的说是服务化,还没完全做到微的程度,颗粒度没那么细),遵循RESTFull规范,使前后端完全分离,实现大前端思想.由于是初次尝 ...

  8. PreparedStatement/Statement处理insert update等操作时乱码,以及URL

    原文: 在顶目中无意中碰到PreparedStatement 在存DB时出现乱码,困扰了好久终于解决问题 问题代码如下 ps = con.prepareStatement(INSERT_SQL); p ...

  9. JS 中new一个对象发生了什么事

    今天看到一个360的前端面试题: function A(){}function B(a){  this.a=a;}function C(a){  if(a){    this.a=a;   }}A.p ...

  10. python 爬取淘宝的模特照片

    前段时间花了一部分时间学习下正则表达式,总觉得利用正则要做点什么事情,所以想通过爬取页面的方式把一些美女的照片保存下来,其实过程很简单. 1.首先读取页面信息: 2.过滤出来照片的url地址: 3.通 ...