Search code examples
javascriptregexnode.jscallback

How can I know the callback is invoked for the last time?


According to String.prototype.replace() When I pass a function to String.replace(), the function will be invoked multiple times, if the regular expression is global.

How can I pass a callback to this callback to know when all invocations are done?


Solution

  • You do not need a callback. String.prototype.replace is a synchronous operation, therefore the code executes sequentially.

    var s = 'test_test_test';
    
    s = s.replace(/test/g, function () { return ''; });
    
    console.log('replace done: ' + s);