56 points · by joeyespo · 6y ago · 67 comments
[1]: https://github.com/tc39/proposal-string-replaceall/issues/16
Chrome: deprecating and removing Custom Elements V0 in 2020.
/* Usage:
> 'abcdefabc'.replaceAll('abc')
'def'
> 'abcdefabc'.replaceAll('abc', '+')
'+def+'
> 'abcdefabc'.replaceAll(/abc/, '+')
'+def+'
> 'abcdefabc'.replaceAll(/abc/g, '+')
'+def+'
*/
String.prototype.replaceAll = function (re_or_str, sub) {
return this.split(re_or_str).join(sub||'')
}
On a side note, is it just me or does the linked polyfill for String.prototype.replaceAll [1] seem wildly complex???[1] https://github.com/es-shims/String.prototype.replaceAll/blob...
// Remove repeated characters
'foobar'.replace(/(.)\1+/g, '$1')
"fobar"
and overriding behaviour with Symbol.replace 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 || "");
}Unlike the string case, the regexp case has a way of communicating that you want to match every instance versus only the first
However, if you just want to replace plain strings, `.replaceAll()` actually gives you a fundamentally better API: you can now use `str.replace(s,r)` for single replace, and the intuitive companion function `str.replaceAll(s,r)` for all-instances replace. No "learning/knowing regexp" required just to replace some plain text.
Just tried `string.match` and `regex.exec` and could only get the first match.
let flagified = new RegExp(original.source, original.flags + "g")
or modify the regex standards as part of the proposal.How do all the other languages that have an equivalent function (Perl quotemeta, PHP preg_quote, Python re.escape, Ruby: Regexp.escape, Java Pattern.quote, C# Regex.Escape, Go QuoteMeta, Rust regex::escape) avoid or deal with this problem?
The first couple of comments as well as the very last one from April in that thread all sum it up quite well.
new RegExp(String.raw`\s`)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...I find myself using a combination of split and join a lot to avoid that.
* They can indicate poor parsing (ie can't read xml with regex) and failure to use programmatic methods (e.g parsing command output rather than making syscalls)
* regexes are very easy to write incorrectly without being obviously wrong
* regexes are often overkill for a direct match
* regexes invite people to make it more complex rather than restructure surrounding code
* regexs are comparatively more expensive than alternative code
* to work on the codebase you are now required to understand regex in general and this particular implementations quirks as well
I often use regexes, and I often choose not to use regexes.
- replace all occurences,
- remove n-th element,
- binary search for an item,
- sort an array without implicit string coercion
in a good stdlib. Lua is explicitly minimalist, so it is not a good example here.
JavaScript... well, remember left-pad? That's what happens when people are forced to re invent the wheels.