Is this usage of hyphen in regex valid?

前端 未结 1 2005
眼角桃花
眼角桃花 2021-01-23 06:17

NB. I only want to know if it\'s a valid application of unescaped hyphen in the regex definition. It\'s not a question about matching email

相关标签:
1条回答
  • 2021-01-23 06:28

    Hyphens inside character class are used for range. However, when put at the beginning or at the end inside character class there is no need of escaping that.

    Note that, in some browsers, hyphens at any position in the character class are still considered as range metacharacters, so it is best practice to always escape it.

    Quoting from regular-expressions.info

    The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. Both [-x] and [x-] match an x or a hyphen. [^-x] and [^x-] match any character that is not an x or a hyphen. Hyphens at other positions in character classes where they can't form a range may be interpreted as literals or as errors. Regex flavors are quite inconsistent about this.

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