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 中工作。这里发生了什么 ?这里有什么问题以及它是如何错误的? 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/39895209/

相关文章:

php - 如何正确使用mysql中的项目ID作为特定项目的链接?

javascript - 用于验证计算并提取数据的正则表达式

javascript - 正则表达式 - 在第二次出现的字符之间匹配字符串

regex - 从 word_separators 中删除了连字符,ctrl+d 不再有意义

javascript - Bootstrap Table Filterby ,按日期范围过滤,不显示任何内容

javascript - 发送时表单无法正常工作

regex - Putty命令查找非罗马字母命名的文件

html - 如何隐藏iframe src链接

html - 多列 ul 中相同高度 li

html - 在段落标记中将两个 HTML 表单放在同一行