5

Consider these two scripts:

1st: " ".match(/(\s)/)

and

2nd: " ".match(/\s/)

Results

1st: [" "," "]

2nd: [" "]

I don't understand this behavior. As far as i knew purpose of capturing group/paranthesis was to have a section of match to be refered again later within regex. But clearly that's not all. Or is this behavior specific to match and split methods.

Techsin
  • 53
  • 3
  • http://stackoverflow.com/questions/10901334/i-cant-accurately-understand-how-does-javascripts-method-string-matchregexp http://stackoverflow.com/questions/6052616/what-does-the-regular-expression-g-mean – Techsin Sep 02 '13 at 17:30

2 Answers2

7

First script: The first result is the whole pattern, the second is the capturing group

Second script: the only result is the whole pattern.

Capturing groups are not only to refer later in the pattern, they are displayed in results too.

When you use a capturing group with split, the capturing group is returned with results and since the separator is supposed to slice the string, it is normal that you obtain ["", " ", ""] as result with
" " as input string and /(\s)/ as pattern.

More informations about split.

When you write " ".match(/(\s)/) the result returned is the first match. This result is unique and contains:

  • the whole match
  • capturing group(s)
  • index of the match
  • input string

When you write " ".match(/(\s)/g) the result returned is all the matches:

  • whole match 1
  • whole match 2
  • etc.

(in the present case you have only one match)

This behaviour is normal. The match method as two different behaviours (with or without /g). It is a kind of two functions in one. For comparison in PHP (or other languages) which doesn't have the g modifier, you have two different functions: preg_match and preg_match_all

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • i'm confused. the 2nd result belongs to 2nd script which has no capturing group. – Techsin Sep 02 '13 at 16:50
  • i see...but how does this play out with split. " ".split(/\s/) => ["", ""] ..... " ".split(/(\s)/) => ["", " ", ""] – Techsin Sep 02 '13 at 16:54
  • 1
    Have you tried reading the documentation of `.split()`? Doesn't it say what it does with the capture groups? – Barmar Sep 02 '13 at 16:54
  • ok ill return with new information...but while i do that for split. Explain this, Why adding /g like this `" ".match(/(\s)/g)` removes the capturing group from result. why is the capturing group no longer displayed in result? – Techsin Sep 02 '13 at 16:59
  • ill just ask a new question i feel this question as is has been completely answered. – Techsin Sep 02 '13 at 17:11
  • 1
    no need i found it here http://stackoverflow.com/questions/10901334/i-cant-accurately-understand-how-does-javascripts-method-string-matchregexp – Techsin Sep 02 '13 at 17:24
5

Capturing groups serve two purposes. They can be referred later in the regexp (or in the replacement string when using .replace()), but they are also returned by the matching function so they can be used by the caller. This is why .match() returns an array: result[0] is the match for the whole regexp, result[n] is the match for the nth capture group.

string.split splices the matches for capture groups into the resulting array. The documentation says:

If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • can you also explain split part...like if its specific to match, return group, then why split also acts differently. Thanks in advance – Techsin Sep 02 '13 at 16:56
  • Just added the split documentation. – Barmar Sep 02 '13 at 16:57
  • thanks a lot i just realized that. i don't know maybe i should ask a new question or what. in match if i put `/g` after regex, global, why the capturing group isn't returned in array. – Techsin Sep 02 '13 at 17:10
  • See http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex?rq=1 – Barmar Sep 02 '13 at 17:14
  • `if the regexp has the g flag, then it produces an array of all the matches but excludes the capturing groups` http://stackoverflow.com/questions/10901334/i-cant-accurately-understand-how-does-javascripts-method-string-matchregexp – Techsin Sep 02 '13 at 17:25