Reputation: 248
I have email regexp which looks like this:
/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,10})$/
The challange is to allow insert hyphen '-' before @ symbol, but with some restrictions:
1)E-mail can't start with hyphen
2)Hyphen can't be before @ symbol.
3)2 hyphens in a row '--' aren't allowed.
Upvotes: 1
Views: 341
Reputation: 9
You may try this:
^[_a-z0-9]+(\.[_a-z0-9]+)*@(?!.*--)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,10})$
3)2 hyphens in a row '--' aren't allowed.
See https://regex101.com/r/r7iCEN/1
Upvotes: 0
Reputation: 626929
You may replace the first \.
with a [.-]
character class that matches either .
or -
:
^[_a-z0-9]+(?:[.-][_a-z0-9]+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,10}$
^^^^
See the regex demo
I also removed unnecessary groupings and converted capturing groups to non-capturing to streamline matching.
Upvotes: 1