str.replace(/\s/g, '')
/正则表达式/ 这两个斜杠是JS正则表达式的规则
\s 表示字符串中的空字符
g 表示全部匹配
The \s
(lowercase s
) matches a whitespace (blank, tab \t
, form-feed \f
and newline \r
or \n
).
On the other hand, the \S+
(uppercase S
) matches anything that is not matched by \s
, i.e., non-whitespace.
In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example, \w
for word character and \W
for non-word character.
- The
\s
(lowercases
) matches a whitespace (blank, tab\t
, form-feed\f
and newline\r
or\n
). On the other hand, the\S+
(uppercaseS
) matches anything that is not matched by\s
, i.e., non-whitespace. In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example,\w
for word character and\W
for non-word character.