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?
From my limited regex knowledge, I think \b
matches an empty string, and |
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
^
and $
are the beginning and end of the string anchors respectively. |
is used to denote alternates, eg this|that
.\\b
\\b
in most flavor is a "word boundary" anchor. 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:
\\w
and \\W
(either order):
^
and \\w
\\w
\\w
and $
\\w
This is not trivial depending on specification.
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.
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.
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. 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. If empty, not matched.
? matches (pattern2) between zero and one times.
Tested here ("m" is there for multi-line example purposes): https://regex101.com/r/mezfvx/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.