2

After returning true or false with:

return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/.test(str));

Where str is testing123@testing123.testing123 it takes about 25 seconds to complete.

In general, shorter strings take less than 1 second.

This is most likely due to back-tracking. I am not very good with Regex, can someone help me reduce the time it takes to process an email. E.g. it must have letter(s) then @ then letter(s) then . then letter(s) and must not be too long.

George
  • 2,330
  • 3
  • 15
  • 36

6 Answers6

3

Just use

\S+@\S+

Or even (with anchors)

^\S+@\S+$

and actually send an email to that address rather than using a complicated, likely error-prone expression.

Jan
  • 42,290
  • 8
  • 54
  • 79
2

This is the RFC 2822 Standrard for matching emails. It can match 99.9% of emails out today.

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

If you want to just catch syntax errors, you can simply use

\S+@\S+

Taken from one of the answers from another question.

Community
  • 1
  • 1
Jimenemex
  • 3,104
  • 3
  • 24
  • 56
1

Remove ? after each [.-]:

/^\w+(?:[.-]\w+)*@\w+(?:[.-]\w+)*(?:\.\w{2,8})+$/

See the regex demo

In ([.-]?\w+)*, the [.-]? matches 1 or 0 occurrences of . or -, and the whole group pattern gets reduced to (\w+)* after \w+, it causes too many redundant backtracking steps.

Also, it is a good idea to use non-capturing groups if you are only using the grouping construct to quantify a group of subpatterns.

Now, regarding

it must have letter(s) then @ then letter(s) then . then letter(s) and must not be too long

I see others suggest ^\S+@\S+\.\S+$ like solutions, which is a good idea, just make sure you understand that \S matches any char other than whitespace (not just letters). Besides, this does not actually provide the final solution since "must not be too long" condition is not met (+ matches from 1 to quite many occurrences, that is why it is described as 1 or more).

I suggest using the pattern inside an HTML5 pattern attribute and restrict the number of chars a user can type with maxlength attribute:

input:valid {
  color: black;
}
input:invalid {
  color: red;
}
<form name="form1"> 
 <input pattern="\S+@\S+\.\S+" maxlength="256" title="Please enter an email address like name@myhost.com!" placeholder="name@myhost.com"/>
 <input type="Submit"/> 
</form>

NOTE: the pattern regex is compiled by enclosing the pattern with ^(?: and )$, you do not need to use ^ and $ in the regex here. So, pattern="\S+@\S+\.\S+" is translated into:

  • ^(?: (this is added by HTML5) - start of a string (and a non-capturing group starts)
  • \S+ - any 1 or more non-whitespace chars
  • @ - a @ char
  • \S+ - any 1 or more non-whitespace chars
  • \. - a dot
  • \S+ - any 1 or more non-whitespace chars
  • )$ (this is added by HTML5) - the non-capturing group ends and the end of a string is matched.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can use this Regex to check emails :

var emailregex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
Amir H KH
  • 351
  • 2
  • 18
0

replace ()* with ()? PS: Pretty weird expression to match emails:)

mashi
  • 532
  • 3
  • 6
0

An efficient method of matching emails is:

\S+@\S+\.\S+

It's short, matches almost any email, and won't match:

abc@abc

as some of these other answers might.