草庐IT

regex - Firefox 错误 : Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression

coder 2023-08-04 原文

我正在使用正则表达式模式匹配进行 HTML5 表单验证。最新版本的 Firefox 给我一个错误。我只是在 Firefox 46 中才开始看到这个问题。我认为这在早期的 Firefox 版本中不是问题。

Unable to check <input pattern='[\@\%]'> because the pattern is not a valid regexp: invalid identity escape in regular expression

由这个非常简单的测试用例引起:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <form>
    <input pattern="[\@\%]">
  </form>
</html>

为什么转义这些字符会被视为错误?我总是在我的正则表达式中转义不是数字或字母的所有内容。除了这个版本的 Firefox,我从来没有提示过这种类型的转义字符。

当我学习正则表达式时,有人告诉我,除了数字或字母之外的所有内容都可能具有特殊含义。即使现在没有,将来的版本中也可能会出现,因此最好避开它们。这不是真的吗?

是否有我不应该为 Firefox 转义的字符列表?

最佳答案

这是由于以下更改:Bug 1227906 - HTML pattern attribute should set u flag for regular expressions

正如有人已经说过的,您不必转义这些字符。只需使用:

<input pattern="[@%]">

关于regex - Firefox 错误 : Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953775/

有关regex - Firefox 错误 : Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression的更多相关文章

随机推荐