Why and I getting: “Invalid regular expression. Uncaught SyntaxError. Invalid escape.”?

天涯浪子 提交于 2019-11-28 02:20:45

You should not escape the hyphen outside a character class in ES6 regex used with the u flag (the one used by default in pattern regexps in the current versions of Chrome and FF).

Also, the regex in the pattern attribute is anchored by default, remove the redudant ^ and $ and shorten the pattern by using an optional group

pattern="\d{4}-\d{2}(-\d{2})?"

This regex in the HTML5 pattern attribute means:

  • \d{4}-\d{2} - match 4 digits, -, and then 2 digits from the start of string
  • (-\d{2})? - and optionally match a - and then 2 digits at the end of the string.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!