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

  • Home
  • SEARCH
  • 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 294491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:22:18+00:00 2026-05-12T06:22:18+00:00

I have been trying to use a simple jQuery operation to dynamically match and

  • 0

I have been trying to use a simple jQuery operation to dynamically match and store all anchor tags and their texts on the page. But I have found a weird behavior. When you are using match() or exec(), if you designate the needle as a separate RegExp object or a pattern variable, then your query matches only one instance among dozens in the haystack.

And if you designate the pattern like this

match(/needle/gi) 

then it matches every instance of the needle.

Here is my code.

You can even fire up Firebug and try this code right here on this page.

var a = {'text':'','parent':[]}; 

$("a").each(function(i,n) {

    var module = $.trim($(n).text());
    a.text += module.toLowerCase() + ',' + i + ','; 

    a.parent.push($(n).parent().parent()); 

});

var stringLowerCase = 'b';

var regex = new RegExp(stringLowerCase, "gi");
//console.log(a.text);
console.log("regex 1: ", regex.exec(a.text));

var regex2 = "/" + stringLowerCase + "/";
console.log("regex 2: ", a.text.match(regex2));

console.log("regex 3: ", a.text.match(/b/gi));

For me it is returning:

regex 1:  ["b"]
regex 2: null
regex 3: ["b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]

Can anyone explain the root of this behavior?

EDIT: I forgot to mention that for regex1, it doesn’t make any difference whether you add the flags “gi” for global and case insensitive matching. It still returns only one match.

EDIT2: SOlved my own problem. I still don’t know why one regex1 matches only one instance, but I managed to match all instances using the match() and the regex1.

So..this matches all and dynamically!

var regex = new RegExp(stringLowerCase, "gi");
console.log("regex 2: ", a.text.match(regex));
  • 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-05-12T06:22:18+00:00Added an answer on May 12, 2026 at 6:22 am

    This is not unusual behaviour at all. In regex 1 you are only checking for 1 instance of it where in regex 3 you have told it to return all instances of the item by using the /gi argument.

    In Regex 2 you are assuming that “/b/” === /b/ when it doesn’t. “/b/” !== /b/. “/b/” is a string that is searching so if you string has “/b/” in it then it will return while /b/ means that it needs to search between the slashes so you could have “abc” and it will return “b”

    I hope that helps.

    EDIT:

    Looking into it a little bit more, the exec methods returns the first match that it finds rather than all the matches that it finds.

    EDIT:

    var myRe = /ab*/g;
    var str = "abbcdefabh";
    var myArray;
    while ((myArray = myRe.exec(str)) != null)
    {
      var msg = "Found " + myArray[0] + ".  ";
      msg += "Next match starts at " + myRe.lastIndex;
      console.log(msg);
    }
    

    Having a look at it again it definitely does return the first instance that it finds. If you looped through it then would return more.

    Why it does this? I have no idea…my JavaScript Kung Fu clearly isnt strong enough to answer that part

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

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Daily list of what I am going to do. Remove… May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer Would this be what you're looking for? '/([^/]+)/archive/' Captures the… May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer I believe you are looking for the command "pause". It… May 12, 2026 at 10:04 am

Related Questions

On my site a number of operations can take a long time to complete.
EDIT: OK, I believe the following solutions are valid: Use the jQuery AOP plugin.
I used SPRY about a year ago to experiment with the fact that you
I've just learnt a bit jQuery, and am trying to use it for a
I'm trying to decide whether to use a custom ASP.Net Ajax Extender or jQuery

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.