Javascript 邮政信箱正则表达式验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5680050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:25:31  来源:igfitidea点击:

PO Box Regular Expression Validation

javascriptjqueryregex

提问by s15199d

Here's my code, but I can't ever trigger the alert.

这是我的代码,但我永远无法触发警报。

$(document).ready( function (){
    $("[id*='txtAddress1S']").blur(function() {
        var pattern = new RegExp('\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b');
        if ($("[id*='txtAddress1S']").val().match(pattern)) {
            alert('We are unable to ship to a Post Office Box.\nPlease provide a different shipping address.'); 
        }

    });
});

回答by Dathan

I tried several PO Box RegExp patterns found on the internet including the ones posted on Stack Overflow, none of them passed our test requirements. Hence, I posted our RegExp below and our test sets:

我尝试了在 Internet 上找到的几种 PO Box RegExp 模式,包括在 Stack Overflow 上发布的模式,但没有一个通过我们的测试要求。因此,我在下面发布了我们的 RegExp 和我们的测试集:

var poBox = /^ *((#\d+)|((box|bin)[-. \/\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i;

var matches = [ //"box" can be substituted for "bin" 
    "#123", 
    "Box 123", 
    "Box-122", 
    "Box122", 
    "HC73 P.O. Box 217", 
    "P O Box125", 
    "P. O. Box", 
    "P.O 123", 
    "P.O. Box 123", 
    "P.O. Box", 
    "P.O.B 123",
    "P.O.B. 123", 
    "P.O.B.", 
    "P0 Box", 
    "PO 123", 
    "PO Box N", 
    "PO Box", 
    "PO-Box", 
    "POB 123", 
    "POB", 
    "POBOX123",
    "Po Box", 
    "Post 123", 
    "Post Box 123", 
    "Post Office Box 123", 
    "Post Office Box", 
    "box #123", 
    "box 122", 
    "box 123", 
    "number 123", 
    "p box", 
    "p-o box", 
    "p-o-box", 
    "p.o box", 
    "p.o. box", 
    "p.o.-box", 
    "p.o.b. #123", 
    "p.o.b.", 
    "p/o box", 
    "po #123", 
    "po box 123", 
    "po box", 
    "po num123", 
    "po-box", 
    "pobox", 
    "pobox123", 
    "post office box" 
];

var nonMatches = [ 
    "The Postal Road", 
    "Box Hill", 
    "123 Some Street", 
    "Controller's Office", 
    "pollo St.", 
    "123 box canyon rd", 
    "777 Post Oak Blvd", 
    "PSC 477 Box 396", 
    "RR 1 Box 1020" 
]; 

回答by drudge

In javascript, you have to escape your slashes:

在 javascript 中,你必须转义你的斜线:

var pattern = new RegExp('\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b');

Also, you could reduce your pattern a bit by using case-insensitive matching:

此外,您可以通过使用不区分大小写的匹配来稍微减少您的模式:

var pattern = new RegExp('\b[p]*(ost)*\.*\s*[o|0]*(ffice)*\.*\s*b[o|0]x\b', 'i');

Note:Your regex also matches on addresses such as:

注意:您的正则表达式也匹配地址,例如:

  • 123 Poor Box Road
  • 123 Harpo Box Street
  • 贫困箱路123号
  • 哈波箱街123号

I would suggest also checking for a number in the string. Perhaps this pattern from a previous answerwould be of some help:

我建议还检查字符串中的数字。也许先前答案中的这种模式会有所帮助:

var pattern = new RegExp('[PO.]*\s?B(ox)?.*\d+', 'i');

(it won't match on "Post Office" spelled out, or the numeric replacements.. but it's a start.)

(它与拼出的“邮局”或数字替换不匹配......但这是一个开始。)

回答by ridgerunner

With Javascript its easier to use a regex literal like so:

使用 Javascript 更容易使用正则表达式文字,如下所示:

var pattern = /\b(?:p\.?\s*o\.?|post\s+office)\s+box\b/i;

(No backslashes required!)

(不需要反斜杠!)

回答by Jonathan Marzullo

This is what i have used accounting for spaces and case insensitive:

这就是我用来考虑空格和不区分大小写的:

http://regexr.com/3cc2q

http://regexr.com/3cc2q

var pattern = /\bP(ost|ostal)?([ \.]*(O|0)(ffice)?)?([ \.]*Box)?\b/i;
  • fail - po box
  • fail - p.o. box
  • fail - po bo
  • fail - post office box
  • fail - P.O. Box
  • fail - PO. Box
  • fail - PO Box
  • fail - POBox
  • fail - P O Box
  • fail - P.O Box
  • fail - PO
  • fail - Postal Box
  • fail - Post Office Box
  • pass - poop box
  • pass - pony box
  • 失败宝箱
  • 失败宝箱
  • 失败-波波
  • 失败 -邮政信箱
  • 失败 -邮政信箱
  • 失败 - PO。盒子
  • 失败 -邮政信箱
  • 失败 -邮政信箱
  • 失败 -邮政信箱
  • 失败 -邮政信箱
  • 失败 - PO
  • 失败 -邮政信箱
  • 失败 -邮政信箱
  • 通便
  • 小马箱

回答by mohrt

This one is working pretty well for us. (php preg_match)

这个对我们来说效果很好。(php preg_match)

$pattern = '!p(ost)?\.?\s*o(ffice)?\.?(box|\s|$)!i';

回答by anand vishwakarma

Its worked for me,

它对我有用,

var poRegex = /\bP(ost|ostal)?([ \.]*O(ffice)?)?([ \.]*Box)?\b/i;

回答by bukharim96

Here's one that matches 'POB', 'PO Box'and 'Post Office Box'. Pattern:

这是匹配“POB”、“PO Box”和“Post Office Box”的一个。图案:

\\b[PO.|Post\\sOffice]*\\s?B(ox)?.*\\d+\\b.

\\b[PO.|Post\\sOffice]*\\s?B(ox)?.*\\d+\\b.

It's a modification of @drudge's solution.

这是对@drudge解决方案的修改。

回答by mcvnpvsr

The regex provided above is accepting PO box which is correct. Modified Regex not to accept is:

上面提供的正则表达式正在接受正确的邮政信箱。不接受的修改后的正则表达式是:

var pattern = /^ *(?!(#\d+)|((box|bin)[-. \/\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#)|(?![a-zA-Z0-9\x20'#,]) *\d+)/i;

回答by Rocky

We ran into false positive PO Boxes after using @Dathan's answerin production for a few months. This simplified version ensures the pattern is followed by a number so won't match things like "Expo Blvd". It also allows things like "#123" and "B1" commonly found in address2fields for apartment/unit/suite numbers. You can play around with it and add your own test cases here: https://regex101.com/r/7ZUQFl/2

在生产中使用@Dathan 的答案几个月后,我们遇到了误报邮政信箱。此简化版本可确保模式后跟一个数字,因此不会与“Expo Blvd”之类的内容匹配。它还允许在address2公寓/单元/套房编号字段中常见的诸如“#123”和“B1”之类的内容。您可以使用它并在此处添加您自己的测试用例:https: //regex101.com/r/7ZUQFl/2

const re = /^\s*(.*((p|post)[-.\s]*(o|off|office)[-.\s]*(b|box|bin)[-.\s]*)|.*((p|post)[-.\s]*(o|off|office)[-.\s]*)|.*((p|post)[-.\s]*(b|box|bin)[-.\s]*)|(box|bin)[-.\s]*)(#|n|num|number)?\s*\d+/i;
const matches = [
  "post office box 1",
  "post office b 1",
  "post off box 1",
  "post off b 1",
  "post o box 1",
  "post o b 1",
  "p office box 1",
  "p office b 1",
  "p off box 1",
  "p off b 1",
  "p o box 1",
  "p-o-b-1",
  "p.o.b.1",
  "POB1",
  "pob #1",
  "pob num1",
  "pob number1",
  "foo pob1",
  "box #1",
  "po 1",
  "pb 1"
];
const nonMatches = [ 
  "Expo Blvd",
  "Rural Route Box 1",
  "Army Post 1",
  "B1",
  "#1",
  "N1",
  "Number 1",
  "Num 1"
];

回答by Jatto_abdul

I found a pattern that works for most realistic post office addresses. Also had to fall to this after getting false positive with @jonathan-marzullo's answerfor Rural Route Post 1which is not a postal address.

我找到了一种适用于大多数现实邮局地址的模式。也不得不落到这个越来越误报@乔纳森- marzullo的后回答Rural Route Post 1这不是一个邮政地址。

My pattern is

我的模式是

pattern = P([.]?(O|0)[.]?|ost|ostal).((O|0)ffice.)?Box{1}\b/i

Matches for the following cases:

匹配以下情况:

PO Box
P.O. Box
PO. Box
po box
P.o box
po box
p.o. box
post office box
P.O. Box
PO. Box
PO Box
Postal Box
Post Office Box
P.O Box
post office box 1
postal office box 1
postal box 1

Doesn't match the following cases:

不符合以下情况:

post office b 1
post off box 1
post off b 1
post o box 1
post o b 1
p office box 1
p office b 1
p off box 1
p off b 1
p o box 1
p-o-b-1
p.o.b.1
POB
pob #1
pob num1
pob number1
foo pob1
box #1
po 1
pb 1
Expo Blvd
Rural Route Box 1
Army Post 1
postal office 1
poop box
pony box
POBox
P O Box
PO
po bo
Pobox
Post