4 ms·
Your implementation is missing features like capture-based substitions // Remove repeated characters 'foobar'.replace(/(.)\1+/g, '$1') "fobar"
by vimslayer 7y ago
Your implementation is missing features like capture-based substitions
// Remove repeated characters
'foobar'.replace(/(.)\1+/g, '$1')
"fobar"
and overriding behaviour with Symbol.replace
- deleted 7y ago[deleted]
- tmpfile 7y agoYou're right, my implementation was not a drop-in replacement for replace. How about this? String.prototype.replaceAll = function (n, sub) { let t, f = "g"; if (n instanceof RegExp) { t = n.source; f = n.flags; if (!f.includes("g")) f += "g"; } else { t = RegExp.escape(n); } let r = new RegExp(t, f); return this.replace(r, sub || ""); }