转载自:http://hi.baidu.com/manailily/blog/item/ff0f7817a869ba4421a4e934.html
//
项目中 过滤单引号

var obj = document.getElementById("txt_wz");

obj.value = obj.value.replace(new RegExp("'","gm"), "");
//
stringObj.replace(rgExp, replaceText)

参数
   stringObj
   
   必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。
   
   rgExp
   
   必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。
   
   replaceText
   
   必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。
   
   说明
   replace 方法的结果是一个完成了指定替换的 stringObj 对象的复制。
   
   下面任意的匹配变量都能用来识别最新的匹配以及找出匹配的字符串。在需要动态决定替换字符串的文本替换中可以使用匹配变量。
   
   字符 含义
   $$ $ (JScript 5.5 或更新版本)
   $& 指定与整个模式匹配的 stringObj 的部分。 (JScript 5.5 或更新版本)
   $` 指定由 $& 描述的匹配之前的 stringObj 部分。 (JScript 5.5 或更新版本)
   $' 指定由 $& 描述的匹配之后的 stringObj 部分。 (JScript 5.5 或更新版本)
   $n 捕获的第 n 个子匹配,此处 n 为从1到9的十进制一位数。 (JScript 5.5 或更新版本)
   $nn 捕获的第 nn 个子匹配,此处 nn 为从01到99的十进制两位数。 (JScript 5.5 或更新版本)
   
   
   如果 replaceText 为函数,对于每一个匹配的子字符串,调用该函数时带有下面的 m+3 个参数,此处 m 是在 rgExp 中捕获的左括弧的个数。第一个参数是匹配的子字符串。接下来的 m 个参数是查找中捕获的全部结果。第 m+2 个参数是在 stringObj 中匹配出现的偏移量,而第 m+3 个参数为 stringObj。结果为将每一匹配的子字符串替换为函数调用的相应返回值的字符串值。
   
   Replace 方法更新全局 RegExp 对象的属性。
   
   示例
   下面的示例演示了 replace 方法将第一次出现的单词 "The" 替换为单词 "A" 的用法。
   
   function ReplaceDemo(){
    var r, re; // 声明变量。
    var ss = "The man hit the ball with the bat.n";
    ss += "while the fielder caught the ball with the glove.";
    re = /The/g; // 创建正则表达式模式。
    r = ss.replace(re, "A"); // 用 "A" 替换 "The"。
    return(r); // 返回替换后的字符串。
   }
   另外, replace 方法也可以替换模式中的子表达式。 下面的范例演示了交换字符串中的每一对单词:
   
   function ReplaceDemo(){
    var r, re; // 声明变量。
    var ss = "The rain in Spain falls mainly in the plain.";
    re = /(S+)(s+)(S+)/g; // 创建正则表达式模式。
    r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
    return(r); // 返回结果字符串。
   }
   下面的示例(在 JScript 5.5 及更新版本中执行)执行的是从华氏到摄氏的转换,它演示了使用函数作为 replaceText。要想知道该函数是如何工作的,传递一个包含数值的字符串,数值后要紧跟 "F" (例如 "Water boils at 212")。
   
   function f2c(s) {
    var test = /(d+(.d*)?)Fb/g; // 初始化模式。
    return(s.replace
    (test,
    function($0,$1,$2) {
    return((($1-32) * 5/9) + "C");
    }
    )
    );
   }
   document.write(f2c("Water freezes at 32F and boils at 212F."));

js居然不提供replaceAll方法,用for循环又有效率问题,给你一个正则表达式的解决方案

js 代码
String.prototype.replaceAll = function(s1,s2){   
return this.replace(new RegExp(s1,"gm"),s2);   
}

方法: string.replace(new RegExp(oldString,"gm"),newString))

gm     g=global, m=multiLine , 大致上方法就是这样的,可以实现替换全部指定字串

另一个简单的验证JS的方法:

在浏览器地址栏输入
javascript:alert("abcabcabc".replace(new RegExp("a","gm"),"ad"))

这样比较省事 ;)     ,不知道多行的会不会很方便

orgStr.replace(new RegExp(findStr, 'g'), replaceStr)
应该就可以替换所有的了
如果不用正则表达式
orgStr.replace(findStr, replaceStr)只能替换第一个

第一次发现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:用于替换的子字符串。

Js代码
  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>

js replace 与replaceall实例用法相关推荐

  1. html5 replace,js replace 与replaceall实例用法详解

    stringObj.replace(rgExp, replaceText) 参数 stringObj 必选项.要执行该替换的 String 对象或字符串文字.该字符串不会被 replace 方法修改. ...

  2. replace和replaceAll 的用法

    eplace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSe ...

  3. JS Replace 全部替换字符 用法

    <script language="javascript"> var r= "1\n2\n3\n"; //将字母\n替换成分号 alert(r.re ...

  4. html5 replace,js replace函数用法详解

    如何正确使用js replace函数呢? 例如,str = str.replace(",", "\\"); 只替换第一个遇到的",". 一, ...

  5. JS中的replace和replaceAll

    JS中的replace和replaceAll replace: js中的replace方法如果直接使用比如: str.replace("-","!");//把文 ...

  6. JS之replace与replaceAll

    replace(被替换的,替换) replace归属于String对象,用于替换字符串. 有两个参数,第一个参数是被替换的,第二个是替换的.将什么什么替换为什么什么.例如: str.replace(' ...

  7. java replace和replaceAll的区别以及用法

    replace和replaceAll是JAVA中常用的替换字符的方法 public String replace(char oldChar, char newChar)         在字符串中用n ...

  8. html dom createevent,js 中 document.createEvent的用法

    js 中 document.createEvent的用法 更新时间:2010年08月29日 23:22:02   作者: 用该方法创建了 Event 对象以后,必须用上表中所示的初始化方法初始化对象. ...

  9. android String的replace和replaceAll的使用

    今天,讲讲字符串中的替换字符的代码的使用. 一.replace的使用 声明 以下是Java.lang.String.replace()方法的声明 public String replace(char ...

最新文章

  1. LINQ教程二:LINQ操作语法
  2. 网页版python叫什么-用Python爬网页需要了解什么背景知识?
  3. 蓝桥杯 试题 基础练习 十六进制转十进制——5行代码AC
  4. spartan6不能直接把时钟连到IO上
  5. 26. 删除排序数组中的重复项
  6. mysql死锁查询_Mysql 查看死锁,解除死锁 方式
  7. java可以返回微妙吗_Java开发中10个最为微妙的最佳编程实践
  8. 1001.双系统互联的坑
  9. 治愈系英语笔记-4-不带动词的句子
  10. 有时候能讲出来,比沉默要好吧
  11. Java中Object转Map类型,Map转Object类型
  12. 实战Python:利用Python实现基于终端的文本行编辑程序
  13. jvm系列(五):tomcat性能调优和性能监控(visualvm)
  14. PR第三次培训笔记(视频效果 转场)
  15. HTML5网页设计的基本知识-几个概念
  16. eclipse安装超帅主题----darkest dark
  17. cocos2d js 别出白线游戏上线
  18. 如何在手机上新建html文件夹,用手机怎么制作网页
  19. 华为GAUSSDB集成
  20. VSCode中配置Rust时报错:error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1

热门文章

  1. 计算机视觉与深度学习公司
  2. esp01s ntp TM1637 数码管时钟源代码
  3. 基于ZigBee和STM32的智能家居控制系统的设计与实现(四)
  4. shell企业面试题
  5. 单片机实现PT2262解码原理
  6. new/delete与malloc/free的区别
  7. Broadcast Receiver @
  8. 虚拟机centos7克隆
  9. 求1!+2!+3!+…+n!(2种方式)
  10. 今生,让我们记住盖茨!