Negated Shorthand Character Classes

The above three shorthands also have negated versions. \D is the same as [^\d]\W is short for [^\w] and \S is the equivalent of [^\s].
Be careful when using the negated shorthands inside square brackets. [\D\S] is not the same as [^\d\s]. The latter matches any character that is neither a digit nor whitespace. It matches x, but not 8. The former, however, matches any character that is either not a digit, or is not whitespace. Because all digits are not whitespace, and all whitespace characters are not digits, [\D\S] matches any character; digit, whitespace, or otherwise.

Post a Comment

0 Comments