Allow - (dash) in regular expression

后端 未结 3 934
梦毁少年i
梦毁少年i 2020-12-10 10:57

I have the following regular expression but I want the text box to allow the dash character

^[0-9a-zA-Z \\/_?:.,\\s]+$

Anyone know how I ca

相关标签:
3条回答
  • 2020-12-10 11:28

    Simple answer, user \- in character class.

    ^[0-9a-zA-Z\- \/_?:.,\s]+$
    
    0 讨论(0)
  • 2020-12-10 11:40

    Escape it with \ like:

    ^[\-0-9a-zA-Z \/_?:.,\s]+$
    
    0 讨论(0)
  • 2020-12-10 11:43

    The dash needs to be the first/last character in the character class in order to be used literally:

    ^[-0-9a-zA-Z \/_?:.,\s]+$
    
    ^[0-9a-zA-Z \/_?:.,\s-]+$
    

    You could also escape it, if not the first/last:

    ^[0-9a-zA-Z\- \/_?:.,\s]+$
    
    0 讨论(0)
提交回复
热议问题