-3
"& Help is available in the Library, ".match(/(i)/)

This should be returning all the matches for "i". Instead it's returning only 2.

enter image description here

Two questions

  1. It should be returning all the matches as that is suggested by mdn

  2. What does index=7 mean in the array(in the picture)

For reference, Here is the definition that found on mdn

(x)
Matches x and remembers the match. These are called capturing groups.

For example, /(foo)/ matches and remembers "foo" in "foo bar".

The capturing groups are numbered according to the order of left parentheses of capturing groups, starting from 1. The matched substring can be recalled from the resulting array's elements 1, ..., [n] or from the predefined RegExp object's properties $1, ..., $9.

Capturing groups have a performance penalty. If you don't need the matched substring to be recalled, prefer non-capturing parentheses (see below).

Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30
  • "This should be returning all the matches for 'i'" This assumption is incorrect. What led you to believe that this was the case? – zzzzBov Sep 27 '17 at 17:41
  • Why do you think groups make your regex global? I don't understand what part of the MDN description makes you think it will match all instances of the group. If you clarify that, I might be able to give you a correct interpretation of whatever text there confused you. – apsillers Sep 27 '17 at 17:42

1 Answers1

0

The two are the full match and the submatch, which happen to be the same.

What you'll need is a global regex, by adding the g modifier.

console.log("& Help is available in the Library, ".match(/i/g));

I also removed the subgroup since it's equal to the full match.

llama
  • 2,535
  • 12
  • 11