JavaScript 正则表达式空白字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6073637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript regex whitespace characters
提问by beatgammit
I have done some searching, but I couldn't find a definitive list of whitespace characters included in the \s in JavaScript's regex.
我进行了一些搜索,但找不到 JavaScript 正则表达式中 \s 中包含的明确空白字符列表。
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.
我知道我可以依赖空格、换行、回车和制表符作为空格,但我认为由于 JavaScript 传统上仅适用于浏览器,因此 URL 编码的空格以及诸如
和 之类的东西也%20
将得到支持。
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?
JavaScript 的正则表达式编译器到底在考虑什么?如果浏览器之间存在差异,我只真正关心 webkit 浏览器,但知道任何差异会很高兴。另外,node.js 怎么样?
采纳答案by Kinlan
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).
HTML != Javascript。Javascript 完全是文字,%20 是 %20 并且
是一串字符串 & nbsp 和 ;。对于字符类,我认为几乎所有 perl 中的 RegEx 都适用于 JS(你不能做命名组等)。
http://www.regular-expressions.info/javascript.htmlis the refernece I use.
回答by pimvdb
A simple test:
一个简单的测试:
for(var i = 0; i < 1000; i++) {
if(String.fromCharCode(i).replace(/\s+/, "") == "") console.log(i);
}
The char codes (Chrome):
字符代码(Chrome):
9
10
11
12
13
32
160
回答by Alex K.
回答by Scherbius.com
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].
在Firefox 中\s - 匹配单个空白字符,包括空格、制表符、换页符、换行符。相当于 [ \f\n\r\t\v\u00A0\u2028\u2029]。
For example, /\s\w*/ matches ' bar' in "foo bar."
例如,/\s\w*/ 匹配“foo bar”中的“bar”。
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions