在jQuery源码中理解空格正则表达式

Understanding whitespace regex in jQuery source

本文关键字:空格 正则表达式 jQuery 源码      更新时间:2023-09-26

我只是想了解jQuery的源代码的空白修剪REGEX和遇到以下:

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

现在使用REGEX工具,我理解了以下内容:

/^['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)

上面的描述使REGEX非常容易理解,但仍然考虑实际实现,一些事情没有意义,即

uFEFF为什么一个字符会有这个字符,它和空白有什么关系?xA0到底是什么?

谁能解释一下?你不必给出最详细的答案,简短的回答就可以了。

0xFEFF被称为ZERO WIDTH NO-BREAK SPACE,并且可能不会在某些浏览器上单独使用's0x00A0也一样,没有空格

关于's在ECMA 262 (Javascript的标准)中捕获的更多细节,请参阅本文档。根据该规范,jQuery过于谨慎,因为有问题的字符已经包含在内。