JS中实现replaceAll的方法
http://fuleonardo.iteye.com/blog/339749
第一次发现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);
JS中实现replaceAll的方法相关推荐
- java和js中替换字符串的方法
//java中字符串的替换方法: String a = "吴林飞-1a_,唐姐-2,周帅-3"; String f = a.replaceFirst("-", ...
- js中match、replace方法中使用正则表达式
为什么80%的码农都做不了架构师?>>> js中match.replace方法中使用正则表达式 (2012-06-29 14:23:37) 转载▼ 标签: 字符串 飞雪 正则表 ...
- JS中令人发指的valueOf方法介绍
转载自 JS中令人发指的valueOf方法介绍 彭老湿近期月报里提到了valueOf方法,兴致来了翻了下ECMA5里关于valueOf方法的介绍,如下: 15.2.4.4 Object.protot ...
- vue 懒人_Vue.js 中的实用工具方法【推荐】
收集日常开发中常用到的一些工具方法, 包含 vue 的公用过滤器.公用指令等 (PS: 懒人养成记) 公用自定义过滤器 import Vue from 'vue' import moment from ...
- java有push方法么_[Java教程]js中push和join方法使用介绍
[Java教程]js中push和join方法使用介绍 0 2013-10-09 07:00:17 push和join方法想必大家并不陌生吧,在本文将为大家详细介绍下js中的push和join方法的使用 ...
- js中数组常用的方法总结,包括ES6
原文地址:js中数组常用的方法总结,包括ES6 1.push() 后增 push()方法可以向数组后添加一个新的元素,并返回新数组的长度. 末尾添加,返回长度,改变原数组 var a = [1,2,3 ...
- js中判断数据类型的方法
JS中判断数据类型的方法: 使用typeof操作符. 对一个值使用 typeof 操作符可能返回下列某个字符串,返回的类型都是字符串形式. (1) undefined:如果这个值未定义 (2) boo ...
- JS中 new Date() 各方法的用法
JS中 new Date() 各方法的用法 1.new Date() 参数篇 a.返回类型为国标时间, b.无参数时可以直接返回输出时的时间, c.有参数时则返回对应时间的国标时间, d.日期中间的符 ...
- JS中数组使用的方法
JS中数组使用的方法 数组的介绍 JavaScript Array(数组):属于对象的一种 在句子中的作用就是使用一个单独的变量来存储一系列的值 数组的创建方法 1.字面量方式:var arr=[] ...
最新文章
- 理解特征统计偏差、方差、平均值、中位数、百分数等等
- mysql 原理 ~ DDL之在线DDL
- Python-描述符
- 环形队列PHP,环形队列 - Go语言中文网 - Golang中文社区
- androidSDK配置环境变量
- linux服务器文件索引inodes满了
- 记录 之 在华为NPU上变更镜像
- mysql 中文列索引_MYSQL多列索引
- 遥感、地理空间数据、全国基础数据下载网站大全汇总
- 在小范围内[打表]验证哥德巴赫猜想(洛谷P1579题题解,Java语言描述)
- 上海交大MBA学费与资助
- 经典面试题:Integer c=100,d=100,c==d 一定是false吗?
- 在html中 标记 lt pre gt,HTML pre标记会导致换行符
- 偷偷告诉你,互联网公司理想的技术架构!
- 旁站,子域名,C段的含义
- eclipse查看代码git历史_Eclipse使用教程 : 利用Eclipse中git的插件来查看项目状态 - Break易站...
- Python模块之Shapely
- 计算机大赛横幅标语有趣的,有创意的横幅标语大全
- 云联惠创业经营者认证_广州公安打掉云联惠涉传销组织 零壹财经曾发文警示...
- SpringBoot官方开发工具,热部署和远程调试真带劲