Search code examples
regexhtmlhtml-input

What variation of regular expressions is supported by an input pattern?


What variation of regular expressions are supported by the pattern attribute of an HTML5 <input>? Is it POSIX (BRE or ERE), PCRE, or something else? In particular I would like to know what meta characters, character classes, and subexpressions are supported.


Solution

  • The regular expression language is the same as JavaScript's.

    From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input.

    Or if you prefer:

    The pattern attribute specifies a regular expression against which the control's value, or, when the multiple attribute applies and is set, the control's values, are to be checked.

    If specified, the attribute's value must match the JavaScript Pattern production. [ECMA262]

    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]

    […]

    The compiled pattern regular expression, when matched against a string, must have its start anchored to the start of the string and its end anchored to the end of the string.

    From http://www.w3.org/TR/html5/forms.html#the-pattern-attribute.