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 8024983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:00:59+00:00 2026-06-04T23:00:59+00:00

Exactly what title asks. I’ll provide some examples while explaining my question. Test string:

  • 0

Exactly what title asks. I’ll provide some examples while explaining my question.

Test string:

var test = "#foo# #foo# bar #foo#";

Say, I want to extract all text between # (all foos but not bar).

var matches = test.match(/#(.*?)#/g);

Using .match as above, it’ll store all matches but it’ll simply throw away the capturing groups it seems.

var matches2 = /#(.*?)#/g.exec(test);

The .exec method apparently returns only the first result’s matched string in the position 0 of the array and my only capturing group of that match in the position 1.

I’ve exhausted SO, Google and MDN looking for an answer to no avail.

So, my question is, is there any better way to store only the matched capturing groups than looping through it with .exec and calling array.push to store the captured groups?

My expected array for the test above should be:

 [0] => (string) foo
 [1] => (string) foo
 [2] => (string) foo

Pure JS and jQuery answers are accepted, extra cookies if you post JSFiddle with console.log. =]

  • 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-04T23:01:01+00:00Added an answer on June 4, 2026 at 11:01 pm

    You can use .exec too like following to build an array

    var arr = [],
        s = "#foo# #bar# #test#",
        re = /#(.*?)#/g,
        item;
    
    while (item = re.exec(s))
        arr.push(item[1]);
    
    alert(arr.join(' '));​
    

    Working Fiddle

    Found from Here

    Well, it still has a loop, if you dont want a loop then I think you have to go with .replace(). In which case the code will be like

    var arr = [];
    var str = "#foo# #bar# #test#"
    str.replace(/#(.*?)#/g, function(s, match) {
                               arr.push(match);
                            });
    

    Check these lines from MDN DOC which explains your query about howexec updates lastIndex property I think,

    If your regular expression uses the "g" flag, you can use the exec
    method multiple times to find successive matches in the same string.

    When you do so, the search starts at the substring of str specified by
    the regular expression’s lastIndex property (test will also advance
    the lastIndex property).

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

Sidebar

Related Questions

The question title is a bit strange because I'm not exactly sure how to
I'm trying to understand what exactly the above (in my question's Title) means? This
My question is not clear at title [i can not write it exactly] e.g
Exactly as per the title. Simply in my HomeController I have: string Username =
NOTE: I am not exactly sure how to title or tag this question, so
It is very hard to summarize in the title what the question is exactly
Exactly as the title asks, why is System.getProperty(line.seperator) returning null. From looking around I
Exactly what the title says. Here's my connection string: \SQLEXPRESS;Database=GGDBase;Integrated Security=SSPI;Trusted_Connection=true;Persist Security Info=False; The
Exactly like what the title asks - i am wondering to now wether i
I wasn't sure exactly how to phrase the question Title. I have this block

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.