JavaScript Patterns 3.6 Regular Expression Literal
1. Using the new RegExp() constructor
- // constructor
- var re = new RegExp("\\\\", "gm");
2. Using the regular expression literal
- // regular expression literal
- var re = /\\/gm;
when using the RegExp()constructor, you also need to escape quotes and often you need to double-escape backslashes, as shown in the preceding snippet.
Regular Expression Literal Syntax
• g—Global matching
• m—Multiline
• i—Case-insensitive matching
- var no_letters = "abc123XYZ".replace(/[a-z]/gi, "");
- console.log(no_letters); //
Another distinction between the regular expression literal and the constructor is that the literal creates an object only once during parse time.
- function getRE() {
- var re = /[a-z]/;
- re.foo = "bar";
- return re;
- }
- var reg = getRE(),
- re2 = getRE();
- console.log(reg === re2); // true
// Kaibo(20140602): For now this result should be false in ES5(Tested in Chrome)- reg.foo = "baz";
- console.log(re2.foo); // "baz"
Note
- This behavior has changed in ES5 and the literal also creates new objects. The behavior has also been corrected in many browser environments, so it’s not to be relied on.
- And one last note that calling RegExp() without new(as a function, not as a constructor) behaves the same as with new.
JavaScript Patterns 3.6 Regular Expression Literal的更多相关文章
- [label][翻译][JavaScript Regular Expression]JavaScript Regular Expressions
原文:http://www.javascriptkit.com/javatutors/re.shtml 校验用户的输入是每一个软件开发者的必须要做的事情. 正则表达式与模式 如何在JavaScript ...
- Regular Expression Patterns
Regular Expression Patterns Following lists the regular expression syntax that is available in Pytho ...
- (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]
10].正则表达式 /** * 正则表达式(Regular Expression): * * 用于文本搜索和文本替换 * */ /** * /good/i是一个正则表达式. * good是一个模式(用 ...
- JavaScript Patterns 3.5 JSON
JSON: JavaScript Object Notation {"name": "value", "some": [1, 2, 3]} ...
- Python中的正则表达式regular expression
1 match = re.search(pat,str) If the search is successful, search() returns a match object or None o ...
- Regular Expression Syntax
python的正则表达式 正则表达式的概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规 ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
随机推荐
- sql server 随机读取数据
--sql server 随机读取数据 * FROM [tablename] ORDER BY NEWID() pk from [tablename] ORDER BY NEWID()) --这两个方 ...
- 利用session做国际化引起的old区内存爆满及修复方法
题记:昨天加班打车回家,看见前面有辆路虎在高速上开的巨慢,挡住了我坐的出租车的路,于是就跟司机吐槽了一句:“前面这车怎么这么面啊?”,司机沉默了大概3秒,说了一句富含哲理性的话:“没有面车,只有面人” ...
- BZOJ1087状压DP 解题报告
1087: [SCOI2005]互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的 ...
- R语言简单实现聚类分析计算与分析(基于系统聚类法)
聚类分析计算与分析(基于系统聚类法) 下面以一个具体的例子来实现实证分析.2008年我国其中31个省.市和自治区的农村居民家庭平均每人全年消费性支出. 根据原始数据对我国省份进行归类统计. 原始数据如 ...
- WPF学习之深入浅出话命令
WPF为我们准备了完善的命令系统,你可能会问:"有了路由事件为什么还需要命令系统呢?".事件的作用是发布.传播一些消息,消息传达到了接收者,事件的指令也就算完成了,至于如何响应事件 ...
- Python基础:函数
一.概述 二.声明.定义和调用 三.参数 1.参数传递 2.实参类型 3.形参绑定 四.返回值 五.名字空间与作用域 1.基本概念 2.名字空间 3.作用域 4.总原则 六.高级 1.装饰器 2.生成 ...
- [水]用vb写了个PCB
这学期我们学操作系统,所以得写个PCB. 于是我借鉴了一下windows的PCB,写了这个 Imports System.Runtime.InteropServices ''' <summary ...
- C#语法糖
首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1. ...
- PHP框架Swoole的一个定时器Timer特性
在各种业务型系统中,往往需要服务器在后台扫描相关数据,触发相应的统计.通知等操作. 比如对于一个项目管理系统,需要每天的特定时间内,统计每项任务的执行.到期情况.整个项目的进度等等,根据统计情况,做相 ...
- mysql命令行备份数据库
MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysq ...