去除字符串中空格的正則表達式


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.

  1. 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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM