简体   繁体   English

了解jQuery源中的空白正则表达式

[英]Understanding whitespace regex in jQuery source

I was just trying to understand jQuery source of the white space trim REGEX and came across the following: 我只是想了解空白修剪REGEX的jQuery源,并且遇到了以下问题:

rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,

Now using a REGEX TOOL , i understood the following: 现在使用REGEX TOOL ,我了解以下内容:

/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g
1st Alternative: ^[\s\uFEFF\xA0]+
^ assert position at start of the string
[\s\uFEFF\xA0]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\uFEFF matches the character uFEFF literally (case sensitive)
\xA0 matches the character   with position 0xA0 (160 decimal or 240 octal) in the character set
2nd Alternative: [\s\uFEFF\xA0]+$
[\s\uFEFF\xA0]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\uFEFF matches the character uFEFF literally (case sensitive)
\xA0 matches the character   with position 0xA0 (160 decimal or 240 octal) in the character set
$ assert position at end of the string
g modifier: global. All matches (don't return on first match)

The above description makes the REGEX very easy to understand, but still thinking about the implementation practically, a few things don't make sense , IE 上面的描述使REGEX非常容易理解,但实际上仍在考虑实现,有些事情没有意义,IE

uFEFF why would a sting ever have this character and what does it have to do with white spaces ? uFEFF为什么a会具有此字符,它与空白有什么关系? And also what on earth is xA0 ? 而且xA0到底是xA0

Can anybody explain ? 有人可以解释吗? you don't have to give the most detailed answer a short brief one will do. 您不必给出最详细的答案,只要简短简短即可。

0xFEFF is known as ZERO WIDTH NO-BREAK SPACE and is possibly not caught on some browsers by using \\s alone. 0xFEFF被称为零宽度无 0xFEFF 空格 ,仅使用\\s可能无法在某些浏览器中捕获。 Ditto for 0x00A0 , NO-BREAK SPACE . 同为0x00A0 NO-BREAK SPACE

See this document for some more detail on what is caught by \\s in ECMA 262 (which is the standard for Javascript.) According to that spec, jQuery is being overly cautious since the characters in question are already included. 有关在ECMA 262(这是Javascript的标准)中\\s捕获的内容的更多详细信息,请参阅此文档 。根据该规范,由于所涉及的字符已经包含jQuery,因此jQuery过于谨慎。 Likely this is due to browser compatibility. 这可能是由于浏览器的兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM