Here is the case: I want to find the elements which match the regex...
targetText = \"SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext\"
var reg = /e(.*?)e/g;
var result;
while((result = reg.exec(targetText)) !== null) {
doSomethingWith(result);
}
Three approaches depending on what you want to do with it:
Loop through each match: .match
targetText.match(/e(.*?)e/g).forEach((element) => {
// Do something with each element
});
Loop through and replace each match on the fly: .replace
const newTargetText = targetText.replace(/e(.*?)e/g, (match, $1) => {
// Return the replacement leveraging the parameters.
});
Loop through and do something on the fly: .exec
const regex = /e(.*?)e/g; // Must be declared outside the while expression,
// and must include the global "g" flag.
let result;
while(result = regex.exec(targetText)) {
// Do something with result[0].
}