Why don't the escape characters in the regex match?

  • If I want to match a dot ( .

    ) character , I have to write this regex:

    /\./

The escape character is required to match the character itself.

  1. If I want to match the 'd' character, I have to write this:

    /d/

The character character is not required to match the character itself.

And if I want to match any character ( /./

) or any numeric character ( /\d/

), it's the other way around.

It seems to me that this approach is not very consistent. What are the reasons behind this?

Thank.

+3


source to share


1 answer


The character .

is a reserved regular expression keyword. d

not. You need to include the escape character when you match a period in order to explicitly specify the regex you want to use as a normal match character. d

itself is not a reserved word, so you don't need to avoid it, but it \d

is a reserved word.



I see someone coming up with a regex, it might be a little weird, but .

used so often and I can't think of the time I really need to match the periods it just makes more sense to have one character without backslash.

+5


source







All Articles