简体   繁体   中英

How to use .exec() to access/replace capture groups in globally matched regex?

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.

Is there some reason why you can't use, or don't want to use, String.prototype.replace ? This is exactly what it's intended to do: match and replace. exec only finds matches, without replacing.

* edit: since you say you want low level access, I'm changing my example to show how to use a callback function, and doing a little bit of fancy stuff, like taking the index of the match and adding it in square brackets after the parens. I also added white space captures before and after the matched word to show how multiple capturing groups are handled in this method.

 const text = 'nonsense nonsense relevant nonsense nonsense relevant nonsense nonsense'; const matcher = /(\s+)(relevant)(\s+)/gi; function cb(match, p1, p2, p3, i, fullTxt) { return p1 + '(' + p2 + ')['+i+']' + p3; } let desiredResult = text.replace(matcher, cb); console.log(desiredResult);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM