Javascript正则表达式中[^]的含义是什么? [英] What is meaning of [^] in Javascript regexps?
问题描述
[^ a]
表示除 a
之外的任何其他字符,但是 [^]
的含义是什么(没有以下字符)吝啬的?正如-
在诸如 [-]
这样的情况下失去了字符范围的含义一样,我假设 [^]
将与插入符号匹配.我花了很长时间调试这个问题,才发现至少在Chrome 19中它看起来可以匹配任何东西,换句话说,等同于.
.是否有适用的规范或预期的行为?
[^a]
means any character other than a
, we know, but what does [^]
(with no following characters) mean? Just as -
loses its meaning of character range in cases such as [-]
, I assumed that [^]
would match the caret. I spent way too long debugging this problem, only to find out that at least in Chrome 19 it appears to match anything--in other words, be equivalent to .
. Is there a spec applicable here or what is the expected behavior?
是的,我知道我可以并且应该使用 [\ ^]
.这个问题更多的是病态好奇心.
Yes, I'm aware that I can and probably should use [\^]
. This question is more in the nature of morbid curiosity.
推荐答案
根据JavaScript规范(ES3和ES5), [^]
与任何单个代码单元匹配,与相同[\ s \ S]
, [\ 0- \ uffff]
,(.| \ s)
(请勿使用;与其他语言不同,与.
的不同之处在于,该点与四个换行符( \ r
, \ n
, \ u2028
和 \ u2029
).
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
).
我不建议使用 [^]
或 []
,因为它们不能跨浏览器一致地工作,并且它们会阻止您的正则表达式在其他浏览器中工作编程语言.IE< = 8和更早版本的Safari对空字符类使用传统的(非JavaScript)正则表达式行为.较旧的Opera版本会逆转正确的JavaScript行为,因此 []
可以匹配任何代码单元,而 [^]
则永远不会匹配.传统的正则表达式行为是,字符类中的前导,未转义的]
被视为文字字符,并且不结束字符类.
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.
如果您使用 XRegExp 库,则 []
和 [^]
可以正常且始终跨浏览器运行.XRegExp还添加了 s
(又名dotall或singleline)标志,该标志使点匹配任何代码单元(与正确遵循JavaScript规范的浏览器中的 [^]
相同).).
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).
这篇关于Javascript正则表达式中[^]的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!