15
var pattern = /(?:)/

From my testing, it seems to match everything. Is this the defined behavior?

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
  • 4
    @mini: That isn't true. The reverse question has come up and gotten lots of attention. – SLaks May 23 '11 at 01:29
  • 1
    I could imagine a system where the user is expected to supply a regex string for matching "something" and his code encapsulates that regex into a `(?:...)`, and then evals it. In fact somebody asked about this case not long ago; they were letting people supply regexes at runtime. – Cheeso May 23 '11 at 01:29
  • @SLaks: What's the reverse question? – Ry- May 23 '11 at 01:30
  • A regex that matches nothing. I'm trying to find it. – SLaks May 23 '11 at 01:31
  • @SLaks: You mean `/^$/`? – Ry- May 23 '11 at 01:35
  • A regex that matches nothing? Like, `/^$/`? Aren't there better ways to do that (e.g. comparing to empty string) – Zirak May 23 '11 at 01:36
  • @mini: Almost. He didn't want to match _anything_. (I answered `/$.^/`) The question was deleted, though. @Zirak: He wanted it for a validation framework, I think. – SLaks May 23 '11 at 01:36
  • @SLaks: Even so, that's a different question. I figured this one is just... unproductive. – Ry- May 23 '11 at 01:37
  • Does an OP have to justify the reason behind every question they ask? – eremzeit Dec 03 '15 at 10:21
  • 1
    Both `/.^/` and `/$./` mismatch any string. Note that `/$^/` still matches the empty string because the beginning is also the ending. – jdh8 Aug 02 '17 at 20:54

2 Answers2

18

This doesn't directly answer the question, but here's what the spec has to say about the empty regular expression:

From 15.5.4.14 String.prototype.split (separator, limit)

The value of separator may be an empty String, an empty regular expression, or a regular expression that can match an empty String.

And from 7.8.5 Regular Expression Literals

NOTE Regular expression literals may not be empty; instead of representing an empty regular expression literal, the characters // start a single-line comment. To specify an empty regular expression, use: /(?:)/ .

So given that it is an accepted value for the separator in .split(), I would guess that it is the defined behavior as a way to split on every character.

"fjeij;als#%^&é.\n isoij\t;oi`1=+-]\r".split(/(?:)/);

["f", "j", "e", "i", "j", ";", "a", "l", "s", "#", "%", "^", "&", "é", ".", "
", " ", "i", "s", "o", "i", "j", "  ", ";", "o", "i", "`", "1", "=", "+", "-", "]", "
"]
user113716
  • 318,772
  • 63
  • 451
  • 440
9

/(?:)/ matches "nothing", which matches everything. There is nothing in everything. Heh heh.
Yes, I would expect this.

Cheeso
  • 189,189
  • 101
  • 473
  • 713