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 ...
随机推荐
- C#语法糖之 session操作类 asp.net
用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&quo ...
- jquery选择器(原创)<二>
jquery选择器,选择接着学: 前面学习了基本选择器中的CSS选择器,现在学层级选择器: 1.子元素选择器 子元素选择器,用于在给定的父元素下,查找这个父元素下面的所有的子元素,语法格式,如下: $ ...
- PHP实现过滤各种HTML标签
首先分享一些比较常见的 $str=preg_replace("/<s*imgs+[^>]*?srcs*=s*(''|")(.*?)\1[^>]*?/?s*> ...
- 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别
[源码下载] 重新想象 Windows 8 Store Apps (50) - 输入: 边缘手势, 手势操作, 手势识别 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
[源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...
- cURL和HTTPie
http://lingxiankong.github.io/blog/2014/08/19/curl-httpie/ 前两天在网上看到一个号称比cURL更牛逼的命令行工具HTTPie,提供命令行交互方 ...
- 小白学Linux(四)--系统常用命令
这里记录一下基础的系统常用命令,都是日常可能用到的,需要记住的一些命令.主要分为5个模块:关于时间,输出/查看,关机/重启,压缩归档和查找. 时间: date :查看设置当前系统时间,dat ...
- 下面介绍一下 Yii2.0 对数据库 查询的一些简单的操作
下面介绍一下 Yii2.0 对数据库 查询的一些简单的操作 User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的 ...
- maven oracle jdbc jar
1.problem describe: when your dependency jar about oracle use code like this: <!-- oracle-connect ...
- LCA算法倍增算法(洛谷3379模板题)
倍增(爬树)算法,刚刚学习的算法.对每一个点的父节点,就记录他的2k的父亲. 题目为http://www.luogu.org/problem/show?pid=3379 第一步先记录每一个节点的深度用 ...