regex - HTML 输入模式不起作用

标签 regex html forms input

.*(\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}|\d{10}).* 这个模式运行良好。但突然间它最近停止在 chrome 和歌剧中工作。这里发生了什么 ?这里有什么问题,怎么错的? Opera 正在通知无效转义,在 Chrome 中也是如此。当我在 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模式。

Unicode 模式对于哪些字符可以在模式内转义具有更严格的规则。参见 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): ^ $ \ . * + ? ( ) [ ] { } |

同一组字符在 ES6 specs page 中称为 SyntaxCharacter .

因此,您只能在字符类中将 - 转义为特殊字符,并使其成为文字,您可以转义它。在其他任何地方都不能转义。

<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>

关于regex - HTML 输入模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46667891/

相关文章:

java - Java中如何使用节点属性值读取、更新和删除现有的XML文件

javascript - 匹配 float 但不匹配由点分隔的日期

java - 如何摆脱字符串中的数字?

javascript - HTML Accordion 填充

php - Symfony2 Twig 形式的快捷方式 : form_widget

forms - 如何使用 Bootstrap 4 自定义输入文件

python - 在 Beautifulsoup 的 find_all 中使用正则表达式

jquery - Bootstrap 旋转木马标题和指示器在外面

javascript - Html 选择一切正常。但当使用 `space` 选择时会抛出错误

css - 显示自定义复选框的 HTML5 验证消息