HTML 输入模式不起作用 [英] HTML input pattern not working

查看:36
本文介绍了HTML 输入模式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.*(d{3}-d{3}-d{2}-d{2}|d{3}-d{2}-d{2}-d{3}|d{10}).* 这个模式运行良好.但最近它突然停止在 Chrome 和歌剧中工作.这里发生了什么 ?这里有什么问题,它是如何出错的?Opera 正在通知无效的转义,在 chrome 中也是如此.当我在 js 中检查它时它工作正常.

.*(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>

推荐答案

重点是 Chrome 和 Firefox 已经支持 ES6 正则表达式规范,并且默认支持 Unicode 模式.

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

Unicode 模式对哪些字符可以在模式内进行转义有更严格的规则.请参阅此参考资料:

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

IdentityEscape:在 BMP 模式中,许多字符可以用反斜杠作为前缀并被解释为它们自己(例如:如果 u 后面没有四个十六进制数字,它被解释为 u).在仅适用于以下字符的 Unicode 模式中(释放 u 用于 Unicode 代码点转义):^ $ . * + ? ( ) [ ] { } |

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): ^ $ . * + ? ( ) [ ] { } |

同一组字符在 SyntaxCharacterrel="nofollow noreferrer">ES6 规范页面.

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>

这篇关于HTML 输入模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆