Html regex pattern: [\\d\\s-]{3} works but [\\d-\\s]{3} doesn't. Why?

谁说我不能喝 提交于 2019-12-02 07:25:33

The real root cause here is that the regex [\d-\s] is used in the pattern HTML5 attribute, and in the latest versions of Chrome and FireFox is compiled as an ES2015-compatible regex with the u modifier. The consequence is that there are much stricter escaping rules for the Unicode regex patterns.

What it means is whenever a char cannot be parsed unambiguously, it is an error. When a char is escaped, but does not need escaping, it is again an error.

The chars that you may escape in the character class inside a u based regex are +, $, ^, *, (, ), |, \, [, ], ., ?, -, {, } (see this source). If the - is at the start/end of the character class, it still can go unescaped, as it can only be parsed as a literal hyphen there.

In between two shorthand character classes, an unescaped - will produce an error because it is treated as a user error.

So, either place a hyphen at the start/end (it is always the best option), or escape it inside the character class (and never escape it outside of the character class).

You define two different things:

  • [a-z] is a definition of a range - all characters from a to z.
  • [az-] is a definition of a set of three elements - a, z and -.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!