I see more often that people use: [0-9] instead of \d and [A-Za-z0-9_] instead of \w
Is there some inconsistencies between browsers or other drawbacks when using metacharacters? Should i use character ranges instead of corresponding metacharacters?
I see more often that people use: [0-9] instead of \d and [A-Za-z0-9_] instead of \w
Is there some inconsistencies between browsers or other drawbacks when using metacharacters? Should i use character ranges instead of corresponding metacharacters?
No known inconsistencies, but two possible reasons.
First, both \d
and \w
mean different things in different RegExp implementations. Quoting regular-expressions.info:
Since certain character classes are used often, a series of shorthand character classes are available.
\d
is short for[0-9]
. In most flavors that support Unicode,\d
includes all digits from all scripts. Notable exceptions are Java, JavaScript, and PCRE. These Unicode flavors match only ASCII digits with\d
.
So, yes, JavaScript is safe in this regard - but if you mostly deal with other implementations (like C# and Python3), you might just not be aware of this.
Second, some developers might prefer [0-9]
to \d
just because, well, explicit is better than implicit.