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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:44:58+00:00 2026-06-05T00:44:58+00:00

In the book JavaScript: The Good Parts, it explains method string.match(regexp) as below: The

  • 0

In the book “JavaScript: The Good Parts”, it explains method string.match(regexp) as below:

The match method matches a string and a regular expression. How it
does this depends on the g flag. If there is no g flag, then the
result of calling string .match( regexp ) is the same as calling
regexp .exec( string ). However, if the regexp has the g flag, then it
produces an array of all the matches but excludes the capturing
groups:

Then the book provides code example:

var text = '<html><body bgcolor=linen><p>This is <b>bold<\/b>!<\/p><\/body><\/html>';
var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
var a, i;
a = text.match(tags);
for (i = 0; i < a.length; i += 1) {
    document.writeln(('// [' + i + '] ' + a[i]).entityify());
}
// The result is
// [0] <html>
// [1] <body bgcolor=linen>
// [2] <p>
// [3] This is
// [4] <b>
// [5] bold
// [6] </b>
// [7] !
// [8] </p>
// [9] </body>
// [10] </html>

My question is that I can’t understand “but excludes the capturing groups”.

In the code example above, html in the </html> is in a capturing group. And why is it still included in the result array?

And / in the </html> is also in a capturing group. And why is it included in the result array?

Could you explain “but excludes the capturing groups” with the code example above?

Thank you very much!

  • 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-05T00:45:00+00:00Added an answer on June 5, 2026 at 12:45 am

    In the code example above, html in the is in a capturing group. And why is it still included in the result array?

    Because it’s the full match. When he says “but excludes the capture groups” he doesn’t mean from the full match result, just that the contents of the capture groups aren’t reiterated in the array. If the capturing groups were included, you’d see

    // The result is
    // [0] <html>
    // [1]           // From the capture group; nothing here
    // [2] html      // From the capture group
    // [3]           // From the capture group; nothing here
    // ...
    

    And / in the is also in a capturing group. And why is it included in the result array?

    For the same reason as above: It’s part of the overall match, and that’s what’s in the result; the contents of the individual capture groups are not.

    This is easier to understand with a simpler example. Consider this code:

    var s = "test1 test2";
    var re = /(test)(.)/g;
    var r = s.match(re);
    var i;
    for (i = 0; i < r.length; ++i) {
        console.log("[" + i + "]: '" + r[i] + "'");
    }
    

    Because the regular expression has the g flag, only the full matches are included in the array, so we see:

    [0]: 'test1'
    [1]: 'test2'

    In each case, the entry in the array is the full match, which includes the characters that matched within capture groups making up the overall expression.

    If we removed the g flag but didn’t change anything else, we’d get the first full match followed by the contents of the two capture groups:

    [0]: 'test1'    // The full match, including the stuff from each capture group
    [1]: 'test'     // Capture group 0's contents
    [2]: '1'        // Capture group 1's contents

    There, the first entry is the full match; then the second and third are the contents of the capture groups. Note that the contents of the capture gruops

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

Sidebar

Related Questions

I saw in Crockford's Book Javascript: The Good Parts that he does typeof comparison
I am reading the book Javascript: The good parts. Now I am reading chapter
I remember reading in Douglas Crockford's Javascript the Good Parts book that there is
I'm reading Douglas Crockford's book JavaScript: The Good Parts. I can't understand the sentence
I am currently reading the book Javascript: The Good Parts and was playing with
Javascript: The Good Parts is a great book. Often I find myself reading passages
I am reading the book JavaScript-The Good Parts, in chapter 4.14 Curry, the book
I'm reading the book Javascript: The Good Parts. And I'm confused by the following
I started reading JavaScript: The Good Parts book and became confused at first pages
In the book Javascript the good parts, on the opening page of Ch3 on

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.