Say I want to match a "word" character (\w
), but exclude "_", or match a whitespace character (\s
), but exclude "\t". How can I do this?
Negated Character Classes If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.
Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.
Use a negated class including \W or \S.
/[^\W_]/ # anything that's not a non-word character and not _ /[^\S\t]/ # anything that's not a non-space character and not \t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With