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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:42:28+00:00 2026-05-11T01:42:28+00:00

I want to match a portion of a string using a regular expression and

  • 0

I want to match a portion of a string using a regular expression and then access that parenthesized substring:

var myString = 'something format_abc'; // I want 'abc'  var arr = /(?:^|\s)format_(.*?)(?:\s|$)/.exec(myString);  console.log(arr); // Prints: [' format_abc', 'abc'] .. so far so good. console.log(arr[1]); // Prints: undefined  (???) console.log(arr[0]); // Prints: format_undefined (!!!)

What am I doing wrong?


I’ve discovered that there was nothing wrong with the regular expression code above: the actual string which I was testing against was this:

"date format_%A" 

Reporting that "%A" is undefined seems a very strange behaviour, but it is not directly related to this question, so I’ve opened a new one, Why is a matched substring returning "undefined" in JavaScript?.


The issue was that console.log takes its parameters like a printf statement, and since the string I was logging ("%A") had a special value, it was trying to find the value of the next parameter.

  • 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. 2026-05-11T01:42:29+00:00Added an answer on May 11, 2026 at 1:42 am

    Update: 2019-09-10

    The old way to iterate over multiple matches was not very intuitive. This lead to the proposal of the String.prototype.matchAll method. This new method is in the ECMAScript 2020 specification. It gives us a clean API and solves multiple problems. It is in major browsers and JS engines since Chrome 73+ / Node 12+ and Firefox 67+.

    The method returns an iterator and is used as follows:

    const string = 'something format_abc'; const regexp = /(?:^|\s)format_(.*?)(?:\s|$)/g; const matches = string.matchAll(regexp);      for (const match of matches) {   console.log(match);   console.log(match.index) }

    As it returns an iterator, we can say it’s lazy, this is useful when handling particularly large numbers of capturing groups, or very large strings. But if you need, the result can be easily transformed into an Array by using the spread syntax or the Array.from method:

    function getFirstGroup(regexp, str) {   const array = [...str.matchAll(regexp)];   return array.map(m => m[1]); }  // or: function getFirstGroup(regexp, str) {   return Array.from(str.matchAll(regexp), m => m[1]); } 

    In the meantime, while this proposal gets more wide support, you can use the official shim package.

    Also, the internal workings of the method are simple. An equivalent implementation using a generator function would be as follows:

    function* matchAll(str, regexp) {   const flags = regexp.global ? regexp.flags : regexp.flags + "g";   const re = new RegExp(regexp, flags);   let match;   while (match = re.exec(str)) {     yield match;   } } 

    A copy of the original regexp is created; this is to avoid side-effects due to the mutation of the lastIndex property when going through the multple matches.

    Also, we need to ensure the regexp has the global flag to avoid an infinite loop.

    I’m also happy to see that even this StackOverflow question was referenced in the discussions of the proposal.

    original answer

    You can access capturing groups like this:

    var myString = 'something format_abc'; var myRegexp = /(?:^|\s)format_(.*?)(?:\s|$)/g; var myRegexp = new RegExp('(?:^|\\s)format_(.*?)(?:\\s|$)', 'g'); var matches = myRegexp.exec(myString); console.log(matches[1]); // abc

    And if there are multiple matches you can iterate over them:

    var myString = 'something format_abc'; var myRegexp = new RegExp('(?:^|\\s)format_(.*?)(?:\\s|$)', 'g'); match = myRegexp.exec(myString); while (match != null) {   // matched text: match[0]   // match start: match.index   // capturing group n: match[n]   console.log(match[0])   match = myRegexp.exec(myString); }

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • 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 I finally have the answer now. All I had to… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer Philippe has talked about the sequence side of things -… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer If data is too long for GET, what about using… May 12, 2026 at 12:58 am

Related Questions

I've got a string like foo (123) bar I want to retrieve all numbers
I've seen lots of examples of making an entire regular expression case-insensitive. What I'm
Here's what Im trying to do. I have a hyperlink like this on my
I need to store large amounts of metering data in a database. A record

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.