去除字符串中空格的正则表达式


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