禁止空白正则表达式

时间:2016-11-04 11:43:35

标签: regex special-characters whitespace

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

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

现在我想知道如何修改它以禁止空格。我尝试了以下但是没有工作

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

1 个答案:

答案 0 :(得分:1)

要禁止字符串中的任何空格,请将\s添加到角色类:

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

模式现在将匹配:

  • ^ - 字符串开头
  • [^<>〜/ @#} $%:;)(_ ^!{&安培; * = |&#39 + \ S] + - 1 or more (due to + at the end) characters *other than those* (as it is a negated character class due to [^在字符类
  • 中定义的...]“表示法”
  • $ - 字符串结束。
相关问题