Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

regex - HTML input pattern not working

.*(d{3}-d{3}-d{2}-d{2}|d{3}-d{2}-d{2}-d{3}|d{10}).* this pattern was working fine. But suddenly it stop working in chrome and opera lately. What's going on here ? What a problem is here and how it's wrong? Opera is informing about invalid escape, same in chrome. It works fine when im checking it in js.

<form>
<input type="text" pattern=".*(d{3}-d{3}-d{2}-d{2}|d{3}-d{2}-d{2}-d{3}|d{10}).*">
<button>
Send
</button>
</form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The point is that Chrome and Firefox already support ES6 regex specifications and support the Unicode mode by default.

Unicode patterns have stricter rules as to what characters can be escaped inside the pattern. See this reference:

IdentityEscape: In BMP patterns, many characters can be prefixed with a backslash and are interpreted as themselves (for example: if u is not followed by four hexadecimal digits, it is interpreted as u). In Unicode patterns that only works for the following characters (which frees up u for Unicode code point escapes): ^ $ . * + ? ( ) [ ] { } |

The same set of chars is referred to as SyntaxCharacter in the ES6 specs page.

So, you can only escape the - inside the character class where it is considered a special character and to make it a literal you can escape it. Everywhere else it must not be escaped.

<form>
  <input type="text" pattern=".*(d{3}-d{3}-d{2}-d{2}|d{3}-d{2}-d{2}-d{3}|d{10}).*">
  <input type=Submit>
 </form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...