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. IOS中图片加载的一些注意点

    图片的加载: [UIImage imageNamed:@"home"] //加载 png图片 在ios中获取一张图片只需要写图片名即可 不需要写后缀 默认都是加载.png的图片 但 ...

  2. UICollectionView使用以及与UITableView的区别

    在开始前我们在这先附一段最简单的代码 - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [ ...

  3. mysql depended_query 优化案例一则

    月度利息统计sql优化 原因:写的sql语句复杂,理解起来有难度,另一方面,查询性能比较低 原来的语句如下: SELECT tp.year, tp.month, tp.bid_id, b.`title ...

  4. mysql如何修改所有的definer

    mysql中的definer是什么,有什么作用? 我们在mysql创建view.trigger.function.procedure.event时都会定义一个Definer=‘xxx’,类似如下: C ...

  5. "数学口袋精灵"bug(团队)

    团队名:MY-HR 成员: 学号 博客园 团队贡献分 丘惠敏(组长) 201406114203 http://www.cnblogs.com/qiuhuimin/ 5 郭明茵 201406114204 ...

  6. JS 对象属性相关--检查属性、枚举属性等

    1.删除属性 delete运算符可以删除对象的属性 delete person.age //即person不再有属性age delete person['age'] //或者这样 delete只是断开 ...

  7. easyui-简单用法寄一些属性

    Easyui 总结 优点: A.简单易用 继承 jQuery 简易使用特性,提供高度抽象接口,短期改善网站易用性. B.开源免费 采用 MIT & GPL 双协议授权,轻松满足自由产品至企业产 ...

  8. 浅谈一下缓存策略以及memcached 、redis区别

    缓存策略三要素:缓存命中率   缓存更新策略  最大缓存容量.衡量一个缓存方案的好坏标准是:缓存命中率.缓存命中率越高,缓存方法设计的越好. 三者之间的关系为:当缓存到达最大的缓存容量时,会触发缓存更 ...

  9. MaterialRefreshLayout

    以上就介绍了比SwipeRefreshLayout更漂亮和强大的下拉刷新控件:Android-MaterialRefreshLayout 1.xml <?xml version="1. ...

  10. Android的新虚拟机ART