Regular Expression to Match Non-Whitespace Characters

后端 未结 1 955
执笔经年
执笔经年 2021-01-21 05:32

I need to make a regular expression that matches something like:

JG2144-141/hello

or

!

but not:



        
相关标签:
1条回答
  • 2021-01-21 06:07

    The \S in [A-Za-z0-9-!/\S] makes this character class equal to \S, but you want to make sure all chars in the string are non-whitespace chars. That is why you should wrap the pattern with ^ and $ anchors and add a + quantifier after \S to match 1 or more occurrences of this subpattern.

    You may use

    ^\S+$
    

    See the regex demo

    Details

    • ^ - start of string
    • \S+ - 1 or more non-whitespace chars
    • $ - end of string.
    0 讨论(0)
提交回复
热议问题