简体   繁体   English

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

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

I have the following Regular Expression which matches an email address format:我有以下匹配 email 地址格式的正则表达式:

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

This is used for validation with a form using JavaScript. However, this is an optional field.这用于验证使用 JavaScript 的表单。但是,这是一个可选字段。 Therefore how can I change this regex to match an email address format, or an empty string?因此,如何更改此正则表达式以匹配 email 地址格式或空字符串?

From my limited regex knowledge, I think \b matches an empty string, and |根据我有限的正则表达式知识,我认为\b匹配一个空字符串,并且| means "Or", so I tried to do the following, but it didn't work:表示“或”,所以我尝试执行以下操作,但没有成功:

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

To match pattern or an empty string, use要匹配pattern或空字符串,请使用

^$|pattern

Explanation解释

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

References参考


On \\b\\b

\\b in most flavor is a "word boundary" anchor. \\b在大多数情况下是一个“词边界”锚点。 It is a zero-width match, ie an empty string, but it only matches those strings at very specific places , namely at the boundaries of a word.它是一个零宽度匹配,即一个空字符串,但它只在非常特定的地方匹配那些字符串,即在单词的边界处。

That is, \\b is located:也就是说, \\b位于:

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

References参考


On using regex to match e-mail addresses使用正则表达式匹配电子邮件地址

This is not trivial depending on specification.根据规范,这不是微不足道的。

Related questions相关问题

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 (ie 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! “或”运算符必备!

Here is the Mozilla documentation for JavaScript Regular Expression syntax. 是 JavaScript 正则表达式语法的 Mozilla 文档。

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.无论如何,您都不会阻止虚假电子邮件,这样您也不会阻止有效地址。

\\b matches a word boundary. \\b 匹配单词边界。 I think you can use ^$ for empty string.我认为您可以将 ^$ 用于空字符串。

^$ did not work for me if there were multiple patterns in regex.如果正则表达式中有多个模式, ^$对我不起作用。

Another solution:另一个解决方案:

/(pattern1)(pattern2)?/g

"pattern2" is optional. “pattern2”是可选的。 If empty, not matched.如果为空,则不匹配。

? matches (pattern2) between zero and one times.在零到一次之间匹配 (pattern2)。

Tested here ("m" is there for multi-line example purposes): https://regex101.com/r/mezfvx/1在这里测试(“m”用于多行示例): https://regex101.com/r/mezfvx/1

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

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