Some questions about regular expressions

Hello everyone, I’m currently reviewing regular expressions and I think I may need some clarification in regards to how matches are evaluated. As an example, the following javascript code:

let regText = “Use the Gauss-Seidel method.”;
let regSpecs = /.[aeiou]./ig;
let regMatches = regText.match(regSpecs);
console.log(regMatches);

Outputs ["se ", "he ", "Gau", "Sei", "del", "met", "hod"]

I notice that even though “aus” and “eid” should also be matches they are not. I’m assuming that this has something to do with the fact that they include characters that are also in other matches. So I suppose what I’m wondering is:

1.) Is my understanding of how the regex is working correct? I’m guessing that in this case with the g flag it probably looks along a string until it finds a match, removes the match from the string and then starts at the next highest index from where the match ended. Repeat until the end of the string is reached.

2.) Let’s say I wanted to make sure the “aus” and “eid” were included as well. How would I do this?

  1. Yes. It will not place the same substring into multiple matches.
  2. As far as I know off the top of my head, the only way to get a list of matches with overlapping substrings would be to write multiple, more specific, patterns and then perform a union on the resulting arrays.

As an alternative one can use a non-consuming match such as a positive lookahead with a capturing group, like (?=(.[aeiou].))

At least it’s possible in python’s regex engine - I’m on a train ATM so can’t easily check it works in JS’ engine

Thanks for the replies guys! The union of different sets or arrays is how I was thinking this would have to be handled. Capturing group/ lookaheads/ back references are a new concept to me that I’m just beginning to explore.

My limited understanding and experience with forward and backward refs so far is that they seem to usually be used as a modifying condition for a match search, but can not actually return match values by their nature. Or maybe I am wrong? I’ll have to experiment around with them more. I’d love to see an example.