Javascript 匹配模式或为空字符串的正则表达式

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

Regular expression which matches a pattern, or is an empty string

javascriptregexemail-validationstring

提问by Curt

I have the following Regular Expression which matches an email address format:

我有以下与电子邮件地址格式匹配的正则表达式:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$

This is used for validation with a form using JavaScript. However, this is an optional field. Therefore how can I change this regex to match an email address format, or an empty string?

这用于验证使用 JavaScript 的表单。但是,这是一个可选字段。因此,如何更改此正则表达式以匹配电子邮件地址格式或空字符串?

From my limited regex knowledge, I think \bmatches an empty string, and |means "Or", so I tried to do the following, but it didn't work:

根据我有限的正则表达式知识,我认为\b匹配一个空字符串,|表示“或”,所以我尝试执行以下操作,但没有奏效:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$|\b

回答by polygenelubricants

To match patternor an empty string, use

要匹配pattern或空字符串,请使用

^$|pattern

Explanation

解释

  • ^and $are the beginning and end of the string anchors respectively.
  • |is used to denote alternates, e.g. this|that.
  • ^$分别是字符串锚点的开始和结束。
  • |用于表示替代,例如this|that

References

参考



On \b

\b

\bin most flavor is a "word boundary" anchor. It is a zero-width match, i.e. an empty string, but it only matches those strings at very specific places, namely at the boundaries of a word.

\b在大多数风味中是一个“词边界”锚。它是一个零宽度匹配,即一个空字符串,但它只在非常特定的地方匹配那些字符串,即在单词的边界处。

That is, \bis located:

也就是说,\b位于:

  • Between consecutive \wand \W(either order):
    • i.e. between a word character and a non-word character
  • Between ^and \w
    • i.e. at the beginning of the string if it starts with \w
  • Between \wand $
    • i.e. at the end of the string if it ends with \w
  • 在连续\w\W(任一顺序)之间:
    • 即在单词字符和非单词字符之间
  • ^和之间\w
    • 即在字符串的开头,如果它以 \w
  • \w和之间$
    • 即在字符串的末尾,如果它以 \w

References

参考



On using regex to match e-mail addresses

使用正则表达式匹配电子邮件地址

This is not trivial depending on specification.

根据规范,这不是微不足道的。

Related questions

相关问题

回答by Jgonzalez731

An alternative would be to place your regexp in non-capturing parentheses. Then make that expression optional using the ?qualifier, which will look for 0 (i.e. empty string) or 1 instances of the non-captured group.

另一种方法是将您的正则表达式放在非捕获括号中。然后使用?限定符将该表达式设为可选,它将查找未捕获组的 0(即空字符串)或 1 个实例。

For example:

例如:

/(?: some regexp )?/

In your case the regular expression would look something like this:

在您的情况下,正则表达式将如下所示:

/^(?:[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+)?$/

No |"or" operator necessary!

不需要|“或”运算符!

Hereis the Mozilla documentation for JavaScript Regular Expression syntax.

是 JavaScript 正则表达式语法的 Mozilla 文档。

回答by Zano

I'm not sure why you'd want to validate an optional email address, but I'd suggest you use

我不确定您为什么要验证可选的电子邮件地址,但我建议您使用

^$|^[^@\s]+@[^@\s]+$

meaning

意义

^$        empty string
|         or
^         beginning of string
[^@\s]+   any character but @ or whitespace
@         
[^@\s]+
$         end of string

You won't stop fake emails anyway, and this way you won't stop valid addresses.

无论如何,您都不会阻止虚假电子邮件,这样您就不会阻止有效地址。

回答by pritaeas

\b matches a word boundary. I think you can use ^$ for empty string.

\b 匹配单词边界。我认为您可以将 ^$ 用于空字符串。