正则表达式模式=“[\s\.-]”是否应该在“(\s|.|-)”处给我相同的结果

问题描述 投票:0回答:1

我正在使用正则表达式来验证输入到 html 输入中的电话号码。我想要一个三位数、三位数、四位数的模式,用空格、破折号或点分隔。

333-333-3333 should match.
333.333.3333 should match.
333 333 3333 should match.

33 shouldn't match.
333/333/3333 shouldn't match

当我有

pattern="[0-9]{3}[\s.-][0-9]{3}[\s.-][0-9]{4}"
时,它会失败并接受我输入的任何内容。

当我拥有

pattern="[0-9]{3}(\s|.|-)[0-9]{3}(\s|.|-)[0-9]{4}"
时,它就会按照我的预期工作。

(\s|.|-)
不应给出与
[\s.-]

相同的结果
html regex validation input
1个回答
0
投票

直接前进

^\d{3}[. -]\d{3}[. -]\d{4}$

请参阅 regex101.com 上的 演示

© www.soinside.com 2019 - 2024. All rights reserved.