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

  1. 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.
  2. 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的更多相关文章

  1. [label][翻译][JavaScript Regular Expression]JavaScript Regular Expressions

    原文:http://www.javascriptkit.com/javatutors/re.shtml 校验用户的输入是每一个软件开发者的必须要做的事情. 正则表达式与模式 如何在JavaScript ...

  2. Regular Expression Patterns

    Regular Expression Patterns Following lists the regular expression syntax that is available in Pytho ...

  3. (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]

    10].正则表达式 /** * 正则表达式(Regular Expression): * * 用于文本搜索和文本替换 * */ /** * /good/i是一个正则表达式. * good是一个模式(用 ...

  4. JavaScript Patterns 3.5 JSON

    JSON: JavaScript Object Notation {"name": "value", "some": [1, 2, 3]}  ...

  5. Python中的正则表达式regular expression

    1 match = re.search(pat,str)  If the search is successful, search() returns a match object or None o ...

  6. Regular Expression Syntax

    python的正则表达式 正则表达式的概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规 ...

  7. 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 ...

  8. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  9. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

随机推荐

  1. Linux的Cgroup

    为什么要有cgroup Linux系统中经常有个需求就是希望能限制某个或者某些进程的分配资源.也就是能完成一组容器的概念,在这个容器中,有分配好的特定比例的cpu时间,IO时间,可用内存大小等.于是就 ...

  2. mysql中如何把字符串转换成日期类型

    select date_format('2013-03-09','%Y-%m-%d'); select date_format('2013-03-09','%y-%m-%d'); select STR ...

  3. SQL查询语言练习

    USE master GO IF EXISTS (SELECT * FROM sysdatabases WHERE name='MyStudentInfoManage') DROP DATABASE ...

  4. 利用mciSendString播放音频

    最近在写音频播放器,不过有点懒散,开发进度很慢,一天只做了一点点东西.其实就是让程序能播放音频.这个在我大二学winform程序开发时书上有说,那是书上教的是用media player的COM组件,而 ...

  5. SQLServer根据不同前缀生成多套流水号

    --种子表 --@prefix 前缀 --@seed 种子值 create table RefNoSeed( prefix ) unique, seed int ) go --测试表 --@inser ...

  6. 交通银行 Java Socket 服务启动 管理 WINDOWS 版

    按照交通银行提供的无界面启动方法试验了很多次,都没有成功,所以自己动手用C# 知识写了一个. 小工具可以判断 交通银行 JAVA SOCKET 服务是否启动,并可以启动/关闭服务 主要代码如下: 判断 ...

  7. c# 文件另存为代码

    利用.NET中的File.Copy方法 命名空间:System.IO 重载列表:Copy(string sourceFilePath,string targetFilePath) sourceFile ...

  8. 【C#】1.1 第1章学习要点

    分类:C#.VS2015 创建日期:2016-06-14 教材:十二五国家级规划教材<C#程序设计及应用教程>(第3版) 一.配套源程序(VS2015版)的运行截图 VS2015版的配套源 ...

  9. java之StringBuilder类详解

    StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...

  10. DP---Mahjong tree

    HDU  5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenl ...