-3

I'm trying to inject some code into a file by using replace. The problem is if the target code contains any of the replace() function's special character sequences, namely $$, it will do something different than what I expect, which is to just copy the characters exactly as they are.

'replaceme'.replace('replaceme', '$$');

you would think this would result in $$ but it actually returns $

Is there a way to disable this functionality so that it maintains the $$ like I want?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
stackers
  • 2,701
  • 4
  • 34
  • 66
  • 1
    From the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement): *'$$: Inserts a "$"'*. So you wouldn't think that. – pilchard Jan 16 '23 at 22:44
  • 1
    i saw the docs, my question was how to avoid that – stackers Jan 16 '23 at 22:46
  • "if the target code contains any of the replace() function's special character sequences, namely $$, it will do something different than what I expect" – stackers Jan 16 '23 at 22:47
  • @Phil Don't you think this is a duplicate? The problem is exactly the same as in https://stackoverflow.com/questions/9423722/string-replace-weird-behavior-when-using-dollar-sign-as-replacement or https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs – Bergi Jan 17 '23 at 00:12
  • not sure why you care since the question is already answered, but I posted it because there was no questions with a similar title that were googlable. also it's a different use case because I knew about the special patterns, just didn't know how to avoid them via the code (without changing the input) – stackers Jan 17 '23 at 01:16

3 Answers3

2

No idea why the $$ wasn't working, I too am wondered. But you can get the result by using the callback function.

'replaceme'.replace('replaceme', () => '$$');
Adarsh Mohan
  • 1,364
  • 1
  • 9
  • 14
2

If you do not like to apply the replacement string patterns, you can use a replacement function:

'replaceme'.replace('replaceme', (_match) => '$$');
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

This might be a bit hacky, but you can create a new method on String that replaces all $ characters in the replacement string with $$:

function replaceRaw(original, match, text) {
  text = text.replaceAll('$', '$$$');
  return original.replace(match, text);
}

console.log(replaceRaw('replaceme', 'replaceme', '$$'));

Note that $$ in the replacement text will become a single $, so this new method makes all $s in the replace $$ (see this MDN article).

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • I downvoted it when you were adding the method to the String.prototype which in no way added to the answer and only brings up more questions. While you have now removed that your answer still over complicates the concept i think you are trying to convey which could be simply communicated with just `console.log('replaceme'.replace('replaceme', '$$$$'));` – pilchard Jan 17 '23 at 00:57
  • @pilchard I have since changed my code to no longer modify the String.prototype (there was also a typo, but I fixed that too). A previous comment (deleted now, so I forget who) helped point that out too. Thanks for the help though. – Michael M. Jan 17 '23 at 00:59
  • 1
    @pilchard As OP stated in a comment on the original question, the question isn't about *why* it happens, but how to stop it from happening, presumably with a function. – Michael M. Jan 17 '23 at 01:01
  • 1
    My suggestion above isn't about why, but about how to account for it, which is exactly what your function does, except with way more overhead. (though actually you only need 3 `$`s) – pilchard Jan 17 '23 at 01:03