\s
It is obvious that all are matched 空字符
, and the empty characters include 空格
, 制表符(tab)
, and 回车符(\n \r)
! Equivalent to
[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u0020\u3000\ufeff].
For example, /\s\w*/ matches 'sdfsdfz' in "foo sdfsdfz.".
If there are other non-space characters on both sides of the string using \s, it will also be removed. I am really speechless. The whole network is talking about using \s to remove spaces. Can you tell the 空格
difference 空字符
?
The correct one is unicode码
represented by a space
Half-width space (English symbol) \u0020, this is commonly used in the code
Full-width space (Chinese symbol) \u3000,
non-breaking space \u00A0 is used in Chinese articles, mainly used in office, so that a word will not be displayed at the end of a line ,Shortcut key ctrl+shift+space ;
So the regex that only removes spaces from both sides of the string is
return str.replace(/(^\u0020*)|(\u0020*$)/g, "");
And remove the null characters on both sides of the string to use
// return str.replace(/(^\s*)|(\s*$)/g, "");
Idea:
\u0020
: Match spaces
*
: Match multiple
^
: Start
$
: End
|
: Or
/g
: globa, global