var email = '[John Smith] <johnsmith@gmail.com>';
var re1 = /.*<+(.*)+>.*/;
var re2 = /.*\[+(.*)+\].*/;
var address = email.replace(re1, "$1");
var name = email.replace(re2, "$1");
I am finding that the 2nd regex (to get the name) runs super slow. but the first one is fine. Why is this and is there a better way to get the strings that I need?
The reason your regular expressions are slow is because they are horribly written.
Now, lets go on to say why they are bad.
Your first expression has a bunch of unnecessary tokens. Such as the leadning and trailing
.*– they make no difference. Secondly, you have quantified the<0 to inf times. Why? Are you wanting to match<<<<<<<<email>? oremail>? Lastly, you have quantified a repeating group. This is horrible becauseAlright, that’s the first expression. The second one is even worse, even though you just switched
<>for[]. Why you might ask? I’ll tell you why. BECAUSE IT DOES NOT MATCH. Why is this so bad you might ask? Because it generates what we call catastrophic backtracking. Why does it do this you might wonder? I’ll tell you why:.*Will try to match as much as possible. In fact, at first, it will consume the entire string. Obviously that fails, so it backtracks a bunch of times until it can match the first[. Awesome, now the engine has found a match at the first position of the string for the literal[(thus making the.*match nothing). Now the next token,.*will again match everything due to its greedy nature. This does not work, so the engine backtracks. It will keep trying to do this until it matches the string. Problem is, it never will. Because your greedy quantifier is surrounded by a quantified group that requires 1 or more matches.Now, how do you fix this? Well, you could simply remove the
+from behind the group. That would fix it. Your regular expressions would still be horrible, but they would not cause the engine to backtrack a million times. How can we improve it even further? By using negated character classes./\[([^]]+)\] <([^>]+)>/View a demo of the regular expression over here: http://regex101.com/r/wS2jN0
If you had used regex101.com to begin with you would have noticed the backtracking problem immediately: http://regex101.com/r/vB8xB0