junvar
junvar

Reputation: 11604

javascript regex match including non-capturing group

Trying one of the comments from my earlier stackoverflow question (Regex non-escaped quotation marks) led to a new question:

var string = 'hello"ther';
string.match(/(?:[^\\])"/);
// ["o"", index: 4, input: "hello"ther", groups: undefined]

The match includes the o before the ", even though the group has the ?: which I thought made it non-capturing.

https://regex101.com/r/VCt1Ye/5

Upvotes: 1

Views: 1469

Answers (1)

Jonathan Lam
Jonathan Lam

Reputation: 17371

The match includes the entire string that matches the RegExp. A non-capturing group just means that it won't be recognized as a group, which would show up in the groups section, but it will still appear in the matched string.

In other words, here is what you would get with capturing groups:

var string = 'hello"ther';
console.log(string.match(/([^\\])"/));
// ["o\"","o"]

Here, the first element is the entire matched string (still the same, captured or uncaptured). It's just that it also captures the "o" as a separate group, which is not true for an uncapturing group.


It looks like you're trying to escape non-escaped matches. If you want to do that, you can do that like so (using capturing groups):

string = string.replace(/([^\\])(")/, '$1\\$2')

Where group 1 is [^\\] and group 2 is " by putting a slash in front of it.


Edit for clarification: The groups are numbered as follows:

  • matches[0]: the entire matched group
  • matches[1]: the first captured group
  • matches[2]: the second captured group
  • ...

An easy way to tell which group it is is to count opening capturing group parentheses before the group. For example, the group ($([\d,]+) in the regex below:

/Total balance for (\w+): (($([\d,]+)\.(\d{2}))/
                   ^      ^^           ^
                   1      23           4

is matches[3] because it is the third opening parenthesis.

Upvotes: 4

Related Questions