Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:14:33+00:00 2026-06-17T02:14:33+00:00

var email = ‘[John Smith] <johnsmith@gmail.com>’; var re1 = /.*<+(.*)+>.*/; var re2 = /.*\[+(.*)+\].*/;

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:14:34+00:00Added an answer on June 17, 2026 at 2:14 am

    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>? or email>? Lastly, you have quantified a repeating group. This is horrible because

    1. A quantified capturing group will overwrite itself
    2. Due to the statement above, it makes no sense to use a capturing group, thus it uses unnecessary resources.

    Alright, 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON data [ { Name: Tom, Email: tom@gmail.com, Notes: Yea, it's
why this code is not working var name = $(#name).val(); var email = $(#email).val();
$(document).ready(function(){ //global vars var name = $(#username); var email = $(#email); function usernameExists() {
There are 3 properties (example 1): [Bindable] public var name:String; [Bindable] public var email:Number;
I'm struggling with a jQuery Ajax call. Please help! var email = patrick@patrick.com; var
I've tried this two ways. Way 1 function Login() { var email = encodeURIComponent($(#loginemail).val());
it looks very basic thing but i could not figure it out. var email
Considers me@there valid. Is not a TLD required? try { foreach (var email in
I have a jquery function for sending email with ajax request; function sendMail(from,to,subject,message){ var
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.