15
var pattern = /(?:)/

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

10
  • 4
    @mini: That isn't true. The reverse question has come up and gotten lots of attention. Commented May 23, 2011 at 1: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. Commented May 23, 2011 at 1:29
  • @SLaks: What's the reverse question? Commented May 23, 2011 at 1:30
  • A regex that matches nothing. I'm trying to find it. Commented May 23, 2011 at 1:31
  • 1
    Both /.^/ and /$./ mismatch any string. Note that /$^/ still matches the empty string because the beginning is also the ending. Commented Aug 2, 2017 at 20:54

2 Answers 2

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", "=", "+", "-", "]", "
"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This seems reasonably defined, because it would be odd for patterns to match different strings in different contexts.
Try in console new RegExp() :))
9

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.