4 ms·
I would have make the "escapeForRegExp" function built-in (it seems to me the kind of thing should be built-in), perhaps called RegExp.quote. (I would also to a
by zzo38computer 7y ago
I would have make the "escapeForRegExp" function built-in (it seems to me the kind of thing should be built-in), perhaps called RegExp.quote. (I would also to add support for many features of PCRE.)
- snek 7y agoYou can use String.raw: new RegExp(String.raw`\s`) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- zzo38computer 7y agoNo, that is something different.
- snek 7y agoYes, but it works quite well for this task.
- anderskaseorg 7y agoIt has nothing to do with this task. function replaceAll(str: string, pattern: string, replacement: string): string { return str.replace(new RegExp(escapeForRegExp(pattern), "g"), replacement); }
- phpnode 7y agoThere's a proposal for RegExp.escape() which was rejected by TC39 for imho quite silly reasons: https://github.com/benjamingr/RegExp.escape/issues/37 https://github.com/benjamingr/RegExp.escape/issues/37
- pjtr 7y agoI don't understand the reasons. What is the actual problem in this "even-odd problem"? 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?
- scrollaway 7y agoThey don't. The reasons are frankly idiotic. The first couple of comments as well as the very last one from April in that thread all sum it up quite well.
- jpxw 7y agoThis is insane