I'm trying to access all the capture groups to do a replace where we wrap the desired result in parentheses in a situation like this:
(See comments for desired result)
const text = 'nonsense nonsense relevant nonsense nonsense relevant nonsense nonsense';
const matcher = /(.*)(relevant)(.*)/gi // Not sure of my regex, since I can't figure out how to actually test it
let desiredResult; // 'nonsense nonsense (relevant) nonsense nonsense (relevant) nonsense nonsense'
I've seen the while
loop method, but it's always only attending to a single capture group in the matcher
, so I'm not able to glean what I need to understand from those examples
There probably is a duplicate of this question, but I've spent over an hour searching and haven't found it. Many apparently ask the same thing, but the multiple capture groups aspect seems to be ignored in all of them.
I'm open to other suggestions on how to do this, although my use case may become more complex, involving additional capture groups and operations, so I think this is the best approach. I'm aware of matchAll
but I don't see how it solves my issue either. I want low level access to the indices, the matches, the whole shebang.