且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该在html5输入正则表达式模式验证中使用^和$吗?

更新时间:2023-11-11 08:13:58

根据标准,正则表达式锚定在开头和结尾。但是,在实践中(经过测试的FF 15和Chrome 21)它只停留在开始!

According to the standard, the regex is anchored at the start and end. However, in practice (tested FF 15 and Chrome 21) it is anchored at the start only!

因此,如果你想兼容标准和现实,你应该用 $ 锚定你的正则表达式明确。是否使用 ^ 也取决于你 - 没有必要。

So if you want to be compatible both with the standard and reality, you should anchor your regex with a $ explicitly. Whether to use ^ also is up to you - it is not necessary.