0

S = "$answer"
S = S.replace('$answer', "'test$'")
console.log(S)

The code above outputs 'test instead of 'test$' as I would expect it to. What's going on here?

deceze
  • 510,633
  • 85
  • 743
  • 889
  • My workaround is to reverse the quotes as such: `S.replace('$answer', '"test$"')` , yet I don't understand why `$'` behaves that way. – Ilja Nevolin Jun 04 '20 at 12:36
  • 1
    Does this answer your question? [JavaScript replace() method dollar signs](https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs) – Hao Wu Jun 04 '20 at 12:38
  • There's an explanation on `MDN` under `Description` why this is happening - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace - `$'` is considered a special replacement pattern – goto Jun 04 '20 at 12:38
  • thanks @goto1 ! I must've overlooked it. – Ilja Nevolin Jun 04 '20 at 13:11

0 Answers0