-2

According to the document using $’ will insert the part of the string that’s after the match. But I'm unable to get it to work.

let myStr = 'My little Alligator later';
console.log(myStr.replace('gator', '$’'));

I was expecting output to be "My little Alli later" (replacing "gator" with " later", which is a string that comes after the match), but the actual output is "My little Alli$’ later". Why do I see '$’ in the output?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Nag
  • 806
  • 1
  • 13
  • 28

1 Answers1

0

You're using the wrong type of quote. It should be a straight quote, not a curly quote.

let myStr = 'My little Alligator later';
console.log(myStr.replace('gator', "$'"));
Barmar
  • 741,623
  • 53
  • 500
  • 612