用于验证地址的 JavaScript 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3763820/
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 06:03:24  来源:igfitidea点击:

JavaScript Regular Expression to validate an address

javascriptregex

提问by Amen

I am trying to modify this script to accept , - and ' for this regex to validate addresses on a form is it written correctly?

我正在尝试修改此脚本以接受 , - 和 ' 以便此正则表达式验证表单上的地址是否正确编写?

^[a-zA-Z0-9\s\,\''\-]*$

回答by codaddict

It works but there are some redundant escapes.

它有效,但有一些多余的转义。

You need not escape comma,single quote and hyphen. You escape only when the char has a special meaning and you want to use it literally. Inside a char class:

您无需转义逗号、单引号和连字符。仅当字符具有特殊含义并且您想按字面意思使用它时才转义。在 char 类中:

  • -is a meta char, but not when it appears at the start or at the end. In your case it appears at the end so it has lost its special meaning (of range making).
  • ]is a meta char which marks the end of char class. So if you want to make ]part of char class you need to escape it.
  • -是元字符,但不是出现在开头或结尾时。在你的情况下,它出现在最后,所以它失去了它的特殊意义(范围制作)。
  • ]是一个元字符,它标志着字符类的结束。因此,如果您想成为]char 类的一部分,则需要对其进行转义。

So you can write your regex as:

所以你可以把你的正则表达式写成:

^[a-zA-Z0-9\s,'-]*$

回答by Pulkit Aggarwal

Above answered Regex gives false when your input is "House No.-780". The user can add "." to the address, therefore we have to make regex which accepts dot also. You can use this regex

当您的输入是“House No.-780”时,以上回答的正则表达式给出错误。用户可以添加“.” 到地址,因此我们必须制作也接受点的正则表达式。您可以使用此正则表达式

/^[a-zA-Z0-9\s,.'-]{3,}$/ . 

This regex accepts minimum three character and there is no limit on max characters. Characters may include a-z, A-Z alphabets, whitespace, comma(,), dot(.), apostrophe ('), and dash(-) symbols.

此正则表达式接受最少三个字符,并且最大字符数没有限制。字符可能包括 az、AZ 字母、空格、逗号 (,)、点 (.)、撇号 (') 和破折号 (-) 符号。

回答by Matt

In the comments, Daniel Vandersluis has a really good point: Addresses can't be verified by merely a regex. The USPS has an entire division called CASS (Coding Accuracy Support System) dedicated to verifying address data. I work for SmartyStreetswhere we provide CASS-Certified services for people. In fact, if you're truly interested in goodaddresses, I'll help you personally in getting started (it's really easy with Javascript, for example. Just takes a minute or so.)

在评论中,Daniel Vandersluis 有一个非常好的观点:地址不能仅通过正则表达式进行验证。USPS 有一个名为 CASS(编码精度支持系统)的完整部门,专门用于验证地址数据。我在SmartyStreets工作,在那里我们为人们提供 CASS 认证的服务。事实上,如果您真的对好的地址感兴趣,我会亲自帮助您入门(例如,使用 Javascript 真的很容易。只需要一分钟左右。)

回答by Robusto

If you just want to accept any of those characters (or an empty string), then you can modify what you have currently to:

如果您只想接受这些字符(或空字符串)中的任何一个,那么您可以修改您当前拥有的内容:

/^[a-z0-9\s,'-]*$/i

This is by no means a street address validator, however. I suggest you learn more about regular expressions from somewhere. http://www.regular-expressions.info/examples.htmlis a good place to start.

然而,这绝不是街道地址验证器。我建议你从某个地方学习更多关于正则表达式的知识。http://www.regular-expressions.info/examples.html是一个很好的起点。