Why do escape characters in regex mismatch?

前端 未结 1 1633
误落风尘
误落风尘 2021-01-20 17:11
  1. If I want to match the dot symbol (.) I have to write this regex:

    /\\./

Escape character is needed to match the

相关标签:
1条回答
  • 2021-01-20 17:26

    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.

    0 讨论(0)
提交回复
热议问题