-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
  • 38,409
  • 6
  • 31
  • 57
Nag
  • 698
  • 1
  • 12
  • 24

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
  • 669,327
  • 51
  • 454
  • 560