简体   繁体   English

Javascript正则表达式表单验证-边缘案例

[英]Javascript regex form validation -edge cases

The following regular expression accounts for almost all the cases I need to match. 以下正则表达式说明了我需要匹配的几乎所有情况。 Examples below. 下面的例子。

/\bP(ost|ostal)?([ \.]*O(ffice)?)?([ \.]*Box)?\b/ig

For clarification -the string is lifted in full from an address field and being validated by jQuery Validator. 为了澄清起见,该字符串从地址字段中完全取出,并由jQuery Validator进行验证。 My workplace is unable to ship to a PO Box -hence the requirement. 我的工作场所无法运送到邮政信箱-因此有此要求。

Matched correctly 正确匹配

po box
p.o. box
post office box
PO. BOX
Post Box
Postal Box

Not matched correctly 匹配不正确

19 boxton drive
3 box hill road
22 pop box street
121 pony box
15 posting street

These are valid addresses which incorrectly match 这些是不正确匹配的有效地址

92 postal drive
17 post street

This is an invalid address which does not match 这是一个无效地址,不匹配

box 199

Give this one a shot: 试一试:

/\bP(?:\.|o|ost(?:al)?)? ?(?:O(?:\.|ffice)?)? ?Box\b/ig

See: https://regex101.com/r/Eh6kMQ/1/ for an explanation and demo 参见: https : //regex101.com/r/Eh6kMQ/1/了解说明和演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM