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\fand newline\ror\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,\wfor word character and\Wfor non-word character.
