According to the JavaScript specification (ES3 and ES5), [^]
matches any single code unit, the same as [\s\S]
, [\0-\uffff]
, (.|\s)
(don't use that; unlike the others, it relies on backtracking), etc. The difference from .
is that the dot doesn't match the four newline code points (\r
, \n
, \u2028
, and \u2029
).
I don't recommend using [^]
or []
, because they don't work consistently cross-browser, and they prevent your regexes from working in other programming languages. IE <= 8 and older versions of Safari use the traditional (non-JavaScript) regex behavior for empty character classes. Older versions of Opera reverse the correct JavaScript behavior, so that []
matches any code unit and [^]
never matches. The traditional regex behavior is that a leading, unescaped ]
within a character class is treated as a literal character and does not end the character class.
If you use the XRegExp library, []
and [^]
work correctly and consistently cross-browser. XRegExp also adds the s
(aka dotall or singleline) flag that makes a dot match any code unit (the same as [^]
in a browser that correctly follows the JavaScript spec).