2012-09-07

The empty regular expression

This blog post takes a look at the empty regular expression.

The empty regular expression

An empty regular expression matches everything.
    > var empty = new RegExp("");

    > empty.test("abc")
    true
    > empty.test("")
    true
As 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

Interestingly, the prototype object of regular expressions is also a regular expression, the empty regular expression:
    > RegExp.prototype
    /(?:)/
You can use RegExp.prototype like any other regular expression:
    > "abc".match(RegExp.prototype)
    [ '', index: 0, input: 'abc' ]

The regular expression that matches nothing

The empty regular expression has an inverse – the regular expression that matches nothing:
    > var never = /.^/;

    > never.test("abc")
    false
    > never.test("")
    false

Related blog post

6 comments:

Giacomo Trezzi said...

Why would i want to use an empty regexp ? (no trolling intended)

Axel Rauschmayer said...

You don’t need it very often. One use case is as a default value that should match everything.

just curious said...

/.^/ == /$./
hmmm ....

just curiousier + curiousier said...

another empty string match /$^/ - and how 'bout /$.^/ - especially in the context of multi-line matches and/or globally:
/$^/gm.test("")
/$^/gm.test("\n\n\n")
etc.

just curiousier + curiousier said...

these constructs may have utility controlling the greediness of string splitting via .split(/()/) to coerce some null results, but not all of them, in the subarrays sequenced by indices 2*n and 2*n+1

just curiousier + curiousier said...

these constructs may have utility controlling the greediness of string
splitting via .split(/( some regex )/) to coerce some null
results, but not all of them, in the subarrays sequenced by indices 2*n
and 2*n+1


(previous comment erroneously trapped on HTML left angle )

Web Analytics