0

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;
    }
}
Juanma
  • 1
  • 1
  • 1
    There is no way to stop calling a callback function here, but why can't you form a regular expression to do matching until a certain point? Do you have any example where a good written regex can't fix this case? – VisioN Apr 24 '13 at 11:03
  • check this http://stackoverflow.com/questions/3214886/javascript-replace-only-replaces-first-match – Rahul R. Apr 24 '13 at 11:08
  • Surely I could, but that's not the point here, this is just an example. Probably the exact question should be: "how can I stop this callback function doing replacements after a particular match?". The "whatever" string here means a regular expression which can be matched by many strings. – Juanma Feb 28 '17 at 13:55

2 Answers2

1

Replace with original once flag is found:

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 {
        if (incomplete) {
            replacement = <captured original>;
        } 
        else {
            ... compute replacement ...
        }
        return replacement;
    }
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Yes, this would work but it doesn't terminate callback after stop word is matched, so unnecessary comparisons are still taking place. Thanks anyway. I think 100% right answer should deal with callbacks management, just like in http://stackoverflow.com/questions/29403229/cancel-a-callback-in-javascript – Juanma Feb 28 '17 at 14:05
  • @Juanma If you managed to come up with a good solution, don't hesitate to share it by answering your own question. – Klas Lindbäck Feb 28 '17 at 14:33
0

Loop replace while regex to replace is found AND it is found before the stopper string

in code :

var str = " blah blah whatever blah blah whatever i_wanna_stop_str blah blah whatever";
var stopStr = "i_wanna_stop_str";
var searchStr = "whatever";
while (str.indexOf(searchStr) != -1 && str.indexOf(searchStr) < str.indexOf(stopStr)) {
    str = str.replace(/whatever/, "some");
} 
jdnoprada
  • 33
  • 1
  • 7
  • Of course this solves the problem, but it doesn't answer my "stop callback function" question. Thanks anyway. – Juanma Feb 28 '17 at 13:58