How to add hyphen to regex

前端 未结 1 1076
萌比男神i
萌比男神i 2020-11-27 08:38

I have encountered this pattern

(\\w+)

and from http://gskinner.com/RegExr/ site I understand that \\w = match alpha-numeric c

相关标签:
1条回答
  • 2020-11-27 09:28

    You need a character class, denoted by [...]. \w can then be used in the character class and more characters can be added:

    [\w-]
    

    Careful though, if you add more characters to match. The hyphen-minus needs to be first or last in a class to avoid interpreting it as a range (or escape it accordingly).

    The + is a quantifier, so it goes after a token (where the whole character class is a single token [as is \w]):

    ([\w-]+)
    
    0 讨论(0)
提交回复
热议问题