JavaScript String.replace:如何知道上次调用回调函数

JavaScript String.replace: how to know that callback function is invoked for last time?

本文关键字:调用 回调 函数 何知道 String replace JavaScript      更新时间:2023-09-26

根据 String.prototype.replace()当我们向 String.rereplace 提供一个函数时,如果我们的正则表达式是全局的,则该函数将被多次调用。我们如何向这个回调函数提供回调,以便我们知道所有调用都已完成。

您不需要回调。 String.prototype.replace是同步操作,因此代码按顺序执行。

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