0

Java Regex:

String regex = "(?i)^[\\w.-][\\+\\w.-]*+@[\\w.-]+\\.[a-z]{2,4}$";
  Pattern  pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher("[email protected]");
 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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.