37

When I run

/(a)/g.exec('a a a ').length

I get

2

but I thought it should return

3

because there are 3 as in the string, not 2!

Why is that?

I want to be able to search for all occurances of a string in RegEx and iterate over them.

FWIW: I'm using node.js

1

11 Answers 11

44

exec() is returning only the set of captures for the first match, not the set of matches as you expect. So what you're really seeing is $0 (the entire match, "a") and $1 (the first capture)--i.e. an array of length 2. exec() meanwhile is designed so that you can call it again to get the captures for the next match. From MDN:

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test will also advance the lastIndex property).

4
  • 3
    Btw, the entire match is $& in js.
    – Qtax
    Jun 30, 2012 at 0:06
  • Interesting, I didn't even know there were such $ variables in JS. I was just speaking in Perl. :) Good to know, though. Jun 30, 2012 at 0:08
  • @Qtax, are you talking about the replace method, or some other context?
    – goat
    Jun 30, 2012 at 0:31
  • @rambo, in the replacement string only.
    – Qtax
    Jun 30, 2012 at 14:02
34

You could use match instead:

'a a a'.match(/(a)/g).length  // outputs: 3
2
  • 3
    Indeed, match is the ideal tool for this case, provided that the OP doesn't need subgroups from within each match.
    – apsillers
    Jun 30, 2012 at 0:55
  • 2
    Provided there is always at least one match. Otherwise match returns null instead of an empty array.
    – Robert
    May 20, 2014 at 3:12
22
+200

while loop can help you

x = 'a a a a';
y = new RegExp(/a/g);
while(null != (z=y.exec(x))) {
   console.log(z);     // output: object
   console.log(z[0]);  // ouput: "a"
}

If you add counter then you get length of it.

x = 'a a a a';
counter = 0;
y = new RegExp(/a/g);
while(null != (z=y.exec(x))) {
   console.log(z);     // output: object
   console.log(z[0]);  // output: "a"
   counter++;
}
console.log(counter);  // output: 4

This is quite safe, even if it doesn't find any matching then it just exits and counter will be 0

Main intention is to tell how RegExp can be used to loop and get all values from string of same matched RegExp

5
  • 3
    +1 I think this is best solution. .exec returns some extra properties that are not returned by .match, and it is meant to be called iteratively to return each incremental match. FYI you can remove null != from the while loop as a null value will be falsy and exit the loop anyway. Also, Happy Bounty Bingo!
    – KyleMit
    Mar 8, 2019 at 22:00
  • 1
    @KyleMit, Thank you for the Bounty! Yes we can remove null !=. But I feel it will help for few new comers to understand code flow.
    – ajaykools
    Aug 14, 2019 at 11:14
  • Very efficient solution, this one. I did however try this one out using RegExp without the 'g' flag (global). Not using that will produce an infinite loop.
    – andiOak
    Sep 20, 2020 at 4:42
  • @andiOak -- Yes, removing 'g' flag will go into infinite loop because every time the search pointer will re-point to starting point 0.
    – ajaykools
    Oct 15, 2020 at 10:40
  • 1
    y = /a/g will suffice since it's already a regex. Calling new RegExp() is redundant.
    – Kevin Beal
    Nov 24, 2020 at 21:33
8

You are only matching the first a. The reason the length is two is that it is finding the first match and the parenthesized group part of the first match. In your case they are the same.

Consider this example.

var a = /b(a)/g.exec('ba ba ba ');
alert(a);

It outputs ba, a. The array length is still 2, but it is more obvious what is going on. "ba" is the full match. a is the parenthesized first grouping match.

The MDN documentation supports this - that only the first match and contained groups are returned. To find all matches, you'd use match() as stated by mVChr.

4

Code:

alert('a a a'.match(/(a)/g).length);

Output:

3
2

regexp.exec(str) returns the first match or the entire match and the first capture (when re = /(a)/g; ) as mentionned in other answers

const str = 'a a a a a a a a a a a a a';
const re = /a/g;

const result = re.exec(str);
console.log(result);

But it also remembers the position after it in regexp.lastIndex property.

The next call starts to search from regexp.lastIndex and returns the next match.

If there are no more matches then regexp.exec returns null and regexp.lastIndex is set to 0.

const str = 'a a a';
const re = /a/g;

const a = re.exec(str);
console.log('match : ', a, ' found at : ', re.lastIndex);

const b = re.exec(str);
console.log('match : ', b, ' found at : ', re.lastIndex);

const c = re.exec(str);
console.log('match : ', c, ' found at : ', re.lastIndex);

const d = re.exec(str);
console.log('match : ', d, ' found at : ', re.lastIndex);

const e = re.exec(str);
console.log('match : ', e, ' found at : ', re.lastIndex);

That's why you can use a while loop that will stop when the match is null

const str = 'a a a';
const re = /a/g;

while(match = re.exec(str)){
  console.log(match, ' found at : ', match.index); 
}

2

There are several answers already but unnecessarily complicated. The identity check on the result is excessive because it is always either an array or null.

let text = `How much wood would a woodchuck chuck if a woodchuck could chuck wood?`;
let re = /wood/g;
let lastMatch;

while (lastMatch = re.exec(text)) {
  console.log(lastMatch);
  console.log(re.lastIndex);

  // Avoid infinite loop
  if(!re.global) break;
}

You can move the infinite loop guard into the conditional expression.

while (re.global && (lastMatch = re.exec(text))) {
 console.log(lastMatch);
 console.log(re.lastIndex);
}
1

For your example, .match() is your best option. However, if you do need subgroups, you can make a generator function.

function* execAll(str, regex) {
  if (!regex.global) {
    console.error('RegExp must have the global flag to retrieve multiple results.');
  }

  let match;
  while (match = regex.exec(str)) {
    yield match;
  }
}

const matches = execAll('a abbbbb no match ab', /\b(a)(b+)?\b/g);
for (const match of matches) {
  console.log(JSON.stringify(match));
  let otherProps = {};
  for (const [key, value] of Object.entries(match)) {
    if (isNaN(Number(key))) {
      otherProps[key] = value;
    }
  }
  
  console.log(otherProps);
}

While most JS programmers consider polluting a prototype to be bad practice, you could also add this to RegExp.prototype.

if (RegExp.prototype.hasOwnProperty('execAll')) {
  console.error('RegExp prototype already includes a value for execAll.  Not overwriting it.');
} else {
  RegExp.prototype.execAll = 
    RegExp.prototype = function* execAll(str) {
      if (!this.global) {
        console.error('RegExp must have the global flag to retrieve multiple results.');
      }

      let match;
      while (match = this.exec(str)) {
        yield match;
      }
    };
}

const matches = /\b(a)(b+)?\b/g.execAll('a abbbbb no match ab');
console.log(Array.from(matches));

0

Encapsulated into a utility function:

const regexExecAll = (str: string, regex: RegExp) => {
  let lastMatch: RegExpExecArray | null;
  const matches: RegExpExecArray[] = [];

  while ((lastMatch = regex.exec(str))) {
    matches.push(lastMatch);

    if (!regex.global) break;
  }

  return matches;
};

Usage:

const matches = regexExecAll("a a a", /(a)/g);

console.log(matches);

Output:

[
  [ 'a', 'a', index: 0, input: 'a a a', groups: undefined ],
  [ 'a', 'a', index: 2, input: 'a a a', groups: undefined ],
  [ 'a', 'a', index: 4, input: 'a a a', groups: undefined ]
]
0

If you want to iterate throw a regex without using while you can use replace.

Example:

const foo = 'a a a';
foo.replace(/(a)/g, (...regex) => {
  // Do something forEach regex found
  console.log(regex);
});

0

Spread <string>.matchAll() into an Array

If you want to use a regular expression to match (with capture groups) as an array, and chain Array methods on, all you have to do is spread (...) <string>.matchAll() into a []:

const mapped = [...'a b c'.matchAll(/(?<letter>[abc])/g)]
console.log(mapped)
// Outputs: (3) [Array(2), Array(2), Array(2)]

Now I can do whatever on the destructured capture group(s):

const mapped = [...'a b c'.matchAll(/(?<letter>[abc])/g)]
const onlyAorC = mapped.filter(({groups:{letter}}) => ['a','c'].includes(letter))
console.log(onlyAorC)
// Outputs: (2) [Array(2), Array(2)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.