How to add hyphen to regex

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


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]):


Leave a Comment