【问题标题】:Whitespace Regular expression asp.net空格正则表达式 asp.net
【发布时间】:2012-12-05 21:19:25
【问题描述】:

我有一个文本框字段,我想规范输入的内容。

我不希望用户输入多于或少于 6-10 个字符。这是我用于限制字符的正则表达式 ^.{6,10}$ 我让这部分工作,但我也不希望用户输入空格(空格)。现在我能够从输入的开头和输入的结尾检测空格,但如果用户在文本中间输入空格,则不能。见例子。

" testing" = regulator detects the space in the beginning. regex i use ^[^\s].+[^\s]$
"testing " = regulator detects the space in the end. regex i use here is same as abow
"test ing" = regulator does not detect the space in the middle. tried different regex with no luck.

如何创建一个能满足我所有要求的调节器?

【问题讨论】:

    标签: c# asp.net regex whitespace removing-whitespace


    【解决方案1】:

    问题在于您的 . 匹配所有内容

    这样做

    ^[^\s]{6,10}$

    [^\s] 匹配除space 之外的任何字符


    ^\w{6,10}$

    \w 类似于[\da-zA-Z_]

    【讨论】:

      【解决方案2】:

      Some1.Kill.The.DJ 的答案很棒,但根据您的个人知识,您也可以使用以下内容:

      ^\S{6,10}$
      

      \S 匹配除space 之外的任何字符,与[^\s] 匹配的方式相同

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 2021-09-13
        • 2020-04-06
        • 2011-09-09
        • 2020-08-06
        • 2011-08-10
        • 2015-02-06
        相关资源
        最近更新 更多