javascript 街道地址的简单正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21264194/
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
simple regex for street address
提问by dinotom
I know there are a dozen questions at least on this. I am trying to have a simple validation of input for street address using Regex whereby I check for at least two spaces in the input entry. The reason? for the most part us addresses are at least 3 parts, street number, street name, type (lane, drive, ave , st ,etc)
我知道至少在这方面有十几个问题。我正在尝试使用正则表达式对街道地址的输入进行简单的验证,从而检查输入条目中的至少两个空格。原因?在大多数情况下,我们的地址至少包含 3 个部分,街道号码、街道名称、类型(车道、车道、大街、街道等)
I want to alert the user if the entry doesn't match at least that, if it has more than three spaces, meaning it has more names in the address, that's fine but the minimum not being met necessitates an alert. My latest effort is below, and is not working.
如果条目至少不匹配,我想提醒用户,如果它有三个以上的空格,这意味着它在地址中有更多的名字,那很好,但没有达到最低要求需要提醒。我最近的努力如下,但没有奏效。
var addregex = new RegExp("^\d{1,6}0([A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,})$|^\d{1,6}0([A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,})$|^\d{1,6}0([A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,}0[A-Z]{1}[a-z]{1,})$");
if (addregex.test($(this).val())) {
alert('is valid');
address.addClass('isvalid');
address.css("border", "1px solid lightgray");
} else {
address.css("border", "2px solid red");
alert("Are you sure this is a valid street address?");
address.focus();
}
回答by
A simple test is all you need: /^\s*\S+(?:\s+\S+){2}/
您只需要一个简单的测试: /^\s*\S+(?:\s+\S+){2}/
回答by Frank Mohaupt
\w+(\s\w+){2,}
\w+(\s\w+){2,}
Description:
描述:
\w+
=> Alphanumeric, one or more repititions\s
=> Whitespace(\s\w+)
=> A numbered capture group [\s\w+]{2,}
=> at least 2 repititions
\w+
=> 字母数字,一个或多个重复\s
=> 空白(\s\w+)
=> 编号的捕获组 [\s\w+]{2,}
=> 至少 2 次重复
回答by user2472643
I could not get the other guys to work for some reason but this one validates all street addresses I have ever come up with...
由于某种原因,我无法让其他人工作,但这个验证了我曾经想出的所有街道地址......
var value = '12136 Test This Road';
var streetAddRegEx = RegExp('/\d{1,}(\s{1}\w{1,})(\s{1}?\w{1,})+)/g');
streetAddRegEx.test(value);
Basically I made it so it was any set of words more then one proceeding a set of numbers...
基本上我做到了,所以它是任何一组单词,然后是一组数字......
回答by MasterWu
Improved version based on ClasG's answer.
基于 ClasG 的答案的改进版本。
/\d+(\s+\w+){1,}\s+(?:st(?:\.|reet)?|dr(?:\.|ive)?|pl(?:\.|ace)?|ave(?:\.|nue)?|rd|road|lane|drive|way|court|plaza|square|run|parkway|point|pike|square|driveway|trace|park|terrace|blvd)/
回答by Test User
^(?!\s*$)^((?!([p|P][-. ]?[o|O].?[- ]?|post office )[b|B]([ox|OX]))(?!(\`|\~|\!|\@|\#|$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\|\'|\<|\,|\.|\>|\?|\"|\;|\:)).)*$
回答by rgbflawed
Why not?...
为什么不?...
var address="123 Something Street";
if (address.split(" ").length>2) {
alert("valid");
} else {
alert("invalid");
}
回答by SamWhan
You've got some viable suggestion here. I would however like to offer you a regex that I think does what you asked for:
你在这里有一些可行的建议。但是,我想为您提供一个我认为可以满足您要求的正则表达式:
\d+\s+\w+\s+\w+
or, if it's a warning only, you could even add testing of "types" you indicated, like:
或者,如果它只是一个警告,您甚至可以添加对您指出的“类型”的测试,例如:
\d+\s+\w+\s+(?:st(?:\.|reet)?|ave(?:\.|nue)?|lane|dr(?:\.|ive)?)
allowing for abbreviated version with or without full stop. (Add road, court, etc. at will)
允许有或没有句号的缩写版本。(随意添加道路、法院等)
Check it out at regex101.
在regex101 上查看。
Hope this helps,
希望这可以帮助,
Regards
问候
EDIT: Remember to add flag for case insensitive. ;)
编辑:记得为不区分大小写添加标志。;)