问题
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
, andmultiline
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