This blog post takes a look at the empty regular expression.
> var empty = new RegExp(""); > empty.test("abc") true > empty.test("") trueAs you probably know, you should only use the RegExp constructor when you are dynamically creating a regular expression. But how do you create it via a literal, given that you can’t use // (the token that starts a line comment)? This is how:
var empty = /(?:)/;(?:) is an empty non-capturing group. Such a group leaves few traces and thus is a good choice. Even JavaScript itself uses the above representation when displaying an empty regular expression:
> new RegExp("") /(?:)/
> RegExp.prototype /(?:)/You can use RegExp.prototype like any other regular expression:
> "abc".match(RegExp.prototype) [ '', index: 0, input: 'abc' ]
> var never = /.^/; > never.test("abc") false > never.test("") false