JavaScript 正则表达式空白字符
我进行了一些搜索,但找不到 JavaScript 正则表达式中 \s
中包含的空白字符的明确列表。
我知道我可以依赖空格、换行、回车和制表符作为空白,但我认为由于 JavaScript 传统上仅适用于浏览器,也许 URL 编码的空白和类似 和
%20
也将受到支持。
JavaScript 的正则表达式编译器到底考虑了什么? 如果浏览器之间存在差异,我只关心 webkit 浏览器,但很高兴知道任何差异。另外,Node.js 怎么样?
I have done some searching, but I couldn't find a definitive list of whitespace characters included in the \s
in JavaScript's regex.
I know that I can rely on space, line feed, carriage return, and tab as being whitespace, but I thought that since JavaScript was traditionally only for the browser, maybe URL encoded whitespace and things like
and %20
would be supported as well.
What exactly is considered by JavaScript's regex compiler? If there are differences between browsers, I only really care about webkit browsers, but it would be nice to know of any differences. Also, what about Node.js?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个简单的测试:
字符代码(Chrome):
A simple test:
The char codes (Chrome):
对于 Mozilla 来说是这样的;
(Ref)
对于 IE (JScript),其
(参考)
For Mozilla its;
(Ref)
For IE (JScript) its
(Ref)
HTML!= JavaScript。 JavaScript 完全是字面意思,%20 是 %20,
是一串字符 & nbsp 和;。对于字符类,我认为几乎每个 Perl 中的 RegEx 都适用于 JS(你不能执行命名组等)。
http://www.regular-expressions.info/javascript.html 是我的参考使用。
HTML != Javascript. Javascript is completely literal, %20 is %20 and
is a string of characters & n b s p and ;. For character classes I consider nearly every that is RegEx in perl to be applicable in JS (you can't do named groups etc).
http://www.regular-expressions.info/javascript.html is the refernece I use.
这是 primvdb 的答案的扩展,涵盖整个 16 位空间,包括 unicode 代码点值以及与 str 的比较。修剪()。我尝试编辑答案以改进它,但我的编辑被拒绝,所以我不得不发布这个新答案。
识别将作为空白正则表达式
\s
或通过String.prototype.trim()
:列表(V8 中):
Here's an expansion of primvdb's answer, covering the entire 16-bit space, including unicode code point values and a comparison with str.trim(). I tried to edit the answer to improve it, but my edit was rejected, so I had to post this new one.
Identify all single-byte characters which will be matched as whitespace regex
\s
or byString.prototype.trim()
:The list (in V8):
在 Firefox 中 \s - 匹配单个空白字符,包括空格、制表符、换页符、换行符。相当于[\f\n\r\t\v\u00A0\u2028\u2029]。
例如,/\s\w*/ 匹配“foo bar”中的“bar”。
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
In Firefox \s - matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\v\u00A0\u2028\u2029].
For example, /\s\w*/ matches ' bar' in "foo bar."
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions