0

Java Regex:

String regex = "(?i)^[\\w.-][\\+\\w.-]*+@[\\w.-]+\\.[a-z]{2,4}$";
  Pattern  pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher("santo+abc@gmail.com");
 System.out.println(matcher.matches());

JS Regex:

 var regex = /^[\w.-][\w\+.-]*+@[\w.-]+\.[a-z]{2,4}$/i;

But JS regex is not a valid expression. Not sure what I am missing here. Please guide

Esteban P.
  • 2,789
  • 2
  • 27
  • 43
Nick
  • 3
  • 4

2 Answers2

1

*+ is a possessive quantifier. It’s like *, but fails the match if it has to match less than what’s available to it. Since @ doesn’t match [\w\+.-] anyway, though, you can just replace it with *.

var regex = /^[\w.-][\w\+.-]*@[\w.-]+\.[a-z]{2,4}$/i;

Also, don’t use this regex to validate e-mail addresses; it’s incorrect. See Regex validation of email addresses according to RFC5321/RFC5322 for a correct one.

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Try:

/^[\w.-][\w\+.-]*@[\w.-]+\.[a-z]{2,4}$/i

I removed the + after the *, because JavaScript doesn't support possessive quantifier.

You can try this regex here: https://regex101.com/r/FmUF5M/1