Open 41922 opened 1 month ago
There's probably an a.replace( b, c )
somewhere. $&, $1, $2, $3, $4, etc. are special characters in the "c" field and should be escaped. The fix here is probably to run some kind of escape algorithm on "c".
You can use mw.util.escapeRegExp(). Nvm, the replace not the search string.
Stack Overflow says the proper way to escape this is to add a dollar sign in front of every special string. So $&, $1, $2, $3, $4
-> $$&, $$1, $$2, $$3, $$4
https://stackoverflow.com/a/28102904/3480193
Someone's probably written a bulletproof function for this somewhere. I imagine it'd be something like...
function escapeSecondParameterOfRegexReplace( secondParameter ) {
return secondParameter.replace( /(\$(?:\d+|&))/g, '$$$1' );
}
https://regex101.com/r/7yjT4v/1
Todo:
edit: here's some more special ones...
Ah here we go.
https://stackoverflow.com/a/6969486/3480193
function escapeReplacement(string) {
return string.replace(/\$/g, '$$$$');
}
We have that function in morebits: Morebits.string.safeReplace.
or so i think? https://en.wikipedia.org/w/index.php?title=Wikipedia:Articles_for_deletion/Log/2024_August_22&diff=prev&oldid=1241631405