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形式存到对象中. //如果此字符 ...
随机推荐
- selenium + python 登录页面,输入账号、密码,元素定位问题
示例简介: 要求:登录QQ邮箱,输入账号.密码 出现问题:页面中含有iframe框架,因此直接进行元素的查找与操作,出现找不到元素的现象,首先需进行iframe框架的转换,使用switch_to_fr ...
- iOS-工程和工作空间、静态库和框架之间的关系
使用Xcode创建的工程Project是单独分开的,如果想要几个工程同时存在,可以通过创建工作空间Workspace.工作空间是对各工程的集合,工程文件名的后缀为.xcodeproj,工作空间文件名的 ...
- JS上了贼船
本文纯属个人观点,没有引经据典,没有小心求证,just吐槽. 互联网的火热.移动web,带动了前端的飞速发展,js好像搭上了顺风车,身价水涨船高,如日中天. web前端是啥?html + css + ...
- Android ContentProvider详解
1.概述 ContentProvider以Uri的形式对外提供数据,允许其他应用程序访问或者修改数据.也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过 ...
- POJ 1007
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 83069 Accepted: 33428 Descrip ...
- canvas-弧形可拖动进度条
一.效果如下: See the Pen XRmNRK by pangyongsheng (@pangyongsheng) on CodePen. 链接dome 二. 本文是实现可拖动滑块实现的基本,及 ...
- A*算法的理解与简单实现
基本定义 一种寻路算法,特点是:启发式的,效率高,基本思路比较简单. 用途 寻路.在指定的地图上,考虑到地图上的移动代价,找到最优的路径. 核心概念 开表,闭表,估值函数. 开表 开表,记录了当前需要 ...
- Arduino UNO +ESP8266采集数据上传到贝壳网
集成电路设计大赛赛程将至,我现在还是毫无头绪,然后又报了一个互联网+,比赛报了,东西就必须出来,时间很紧的情况下,所以选择了开源的arduino的进行完成.从开始接触Arduino到完成工程,前前后后 ...
- SAP ECC EHP7 RFC 发布成WebService
1.说明介绍 本文将RFC发布成WebService的详细步骤,参考了百度经验http://jingyan.baidu.com/article/8275fc867c9e2946a13cf66c.htm ...
- java虚拟机学习-JVM调优总结-调优方法(12)
JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...