admin管理员组文章数量:1429735
After returning true or false with:
return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/.test(str));
Where str is [email protected] it takes about 25 seconds to plete.
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.
After returning true or false with:
return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,8})+$/.test(str));
Where str is [email protected] it takes about 25 seconds to plete.
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.
Share Improve this question asked Oct 24, 2017 at 13:53 GeorgeGeorge 2,4223 gold badges18 silver badges39 bronze badges 5- 25 seconds? I can't beleive. – Avinash Raj Commented Oct 24, 2017 at 13:55
- Have a look on : stackoverflow./questions/46155/… – Stéphane Ammar Commented Oct 24, 2017 at 13:55
-
1
Not an answer to your question, but this won't match for the valid email
[email protected]
. You shouldn't do more than just check if it contains an@
and then have an email verification process, since a regular expression for emails is really hard. – GeorgDangl Commented Oct 24, 2017 at 13:57 - I did have it allow 16 final chars instead of just 8, but yeah I need to improve it a lot. – George Commented Oct 24, 2017 at 13:59
- Please see my finalized answer. Sorry to stop too early. – Wiktor Stribiżew Commented Oct 24, 2017 at 20:35
6 Answers
Reset to default 3Just use
\S+@\S+
Or even (with anchors)
^\S+@\S+$
and actually send an email to that address rather than using a plicated, likely error-prone expression.
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.
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 [email protected]!" placeholder="[email protected]"/>
<input type="Submit"/>
</form>
NOTE: the pattern regex is piled 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.
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})(\]?)$/;
replace ()* with ()? PS: Pretty weird expression to match emails:)
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.
本文标签: javascriptEmail validation regex takes a long time to complete on mediumlong stringsStack Overflow
版权声明:本文标题:javascript - Email validation regex takes a long time to complete on medium-long strings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745546833a2662740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论