禁止空白正则表达式

Disallow Whitespace Regex

我有一个不允许某些特殊字符的正则表达式。

^[^<>`~!/@\#}$%:;)(_^{&*=|'+]+$

现在我想知道如何修改它以禁止空格。我尝试了以下但不起作用

`^\S[^<>`~!/@\#}$%:;)(_^{&*=|'+]+$`

要禁止字符串中有任何空格,请将 \s 添加到您的字符 class:

^[^<>`~!/@\#}$%:;)(_^{&*=|'+\s]+$
                            ^^

模式现在将匹配:

  • ^ - 字符串开始
  • [^<>~!/@#}$%:;)(_^{&*=|'+\s]+- 1 or more (due to+at the end) characters *other than those* (as it is a negated character class due to[^.. .]` 符号)在字符 class
  • 中定义
  • $ - 字符串结尾。