对于调试(使用 puppeteer/headless Chrome),我想记录页面的所有正则表达式模式(例如,所有参数new RegExp(), //)。我可以window.RegExp用我自己的函数替换,但这不会捕获正则/pattern/表达式。如何进行?
更新:感谢@vlaz,很棒的答案是这样的:
var regex = /abc/;
origtest = RegExp.prototype.test;
RegExp.prototype.test = function (p, f) {
process.stdout.write(this + "\n");
return origtest.apply(this, arguments);
};
console.log(regex.test("bla"));
不能在替换函数中使用console.log,因为console.log本身使用的是正则表达式。