I've found an interesting problem not answered here as far as I know. I have a regex object which I use to make replacements inside a string. The problem is that if I want to stop further replacements because I've found a specific string, I can't do so. The "break" statement obviously is not working because this is nor a loop neither a switch-case.
If I use "return" the replaces will continue. I can use the "incomplete" variable (see example below) at the head of the function to prevent further replacements but checkings will go on, it will be evaluated as many times as the regex is matched, which is not needed.
Is there a way to completely stop this function's replacements?
Thanks.
Example:
var regex = new RegExp("whatever", "g");
str = str.replace(regex, function(tot, group1) {
if (group1 == "i_wanna_stop_str") {
incomplete = true;
break; <-- not working
} else {
... compute replacement ...
return replacement;
}
}