0

consider the following code:

let x='father';

let m=x.match(/fa(.?)(.?)er/g); // x == ["father"]

let m=x.match(/fa(.?)(.?)er/); // x == ["father", "t", "h"]

So with the 'g' global flags, the capturing groups do not match. Why does this happen ?

kofifus
  • 17,260
  • 17
  • 99
  • 173
  • 1
    That's the way it is [*if you want to obtain capture groups and the global flag is set, you need to use `RegExp.exec()` instead.*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match#Using_global_and_ignore_case_flags_with_match()) – revo Sep 18 '16 at 09:27
  • 1
    It was designed so ... If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned. If there were no matches, the method returns null. – passion Sep 18 '16 at 09:38
  • thx passion, if you post an answer I'll approve it – kofifus Sep 18 '16 at 09:43
  • Please do a spell and grammar check on your post title. Also, please follow standard international English conventions, where the first word in a sentence is capitalized, and there is no space before the punctuation ending a sentence. –  Sep 18 '16 at 11:20
  • 1
    Read the [documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match), please. It explains this quite clearly. –  Sep 18 '16 at 11:21

0 Answers0