Reputation: 431
If I want to match the dot symbol (.
) I have to write this regex:
/\./
Escape character is needed to match the symbol itself.
If I want to match the 'd' symbol I have to write this one:
/d/
Escape character is not needed to match the symbol itself.
And if I want to match any character (/./
) or any digit character (/\d/
) it's vice versa.
It seems to me that this approach is not very consistent. What is the reasoning that stands behind it?
Thank you.
Upvotes: 3
Views: 135
Reputation: 2256
The .
character is a reserved regular expression keyword. The d
isn't. You need to include the escape character when you match a period to explicitly tell regex that you want to use the period as a normal matching character. d
by itself isn't a reserved word, so you don't need to escape it, but \d
is a reserved word.
I can see how, to someone coming to regex it can be a little odd, but the .
is used so often, and I can't think of a time I've really needed to match periods it just makes more sense to have it be one character without the backslash.
Upvotes: 5