Is there a way to apply regex modifiers for HTML5 input's pattern-attribute?

喜夏-厌秋 提交于 2021-02-05 05:56:12

问题


There is this snippet from the HTML specification, but either I do not understand the specification or it does not exactly say anything too informative regarding the regex-modifiers.


回答1:


You should check this HTML5 pattern attribute documentation:

If an input element has a pattern attribute specified, and the attribute's value, when compiled as a JavaScript regular expression with the global, ignoreCase, and multiline flags disabled (see ECMA262 Edition 5, sections 15.10.7.2 through 15.10.7.4), compiles successfully, then the resulting regular expression is the element's compiled pattern regular expression. If the element has no such attribute, or if the value doesn't compile successfully, then the element has no compiled pattern regular expression. [ECMA262]

So, there is no way to apply regex modifiers to this attribute.

Just in case you need to use case-insensitive patterns: use character classes, e.g. to match "ball", use pattern="[Bb][Aa][Ll]{2}".

Multiline mode is usually just not necessary as in the majority of cases, a single line string is being checked.

As the regex is anchored by default, there is no point to support g modifier.



来源:https://stackoverflow.com/questions/32742283/is-there-a-way-to-apply-regex-modifiers-for-html5-inputs-pattern-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!