0

I have a string "$$$$$1.00" Need to replace all '$' with empty string. I don't know the exact number of '$' as I fetch it from server.

I tried using

"$$$$$1.00".replace(/$/g, "")

which is not working out.

I know I can run through the loop and remove it. Is there any way to do that. Also, why it is not working ?

It is working in this case

I have added the JSfiddle link for the simple executable JSFiddle

Dexygen
  • 12,041
  • 11
  • 76
  • 145
Uthistran Selvaraj
  • 1,311
  • 1
  • 12
  • 30

2 Answers2

1

You need to escape the dollar sign ($) in the RegExp as this means the end of the string in RegExp world.

Refactor to:

"$$$$$1.00".replace(/\$/g, "")
seantunwin
  • 1,552
  • 14
  • 15
  • 1
    @UthistranSelvaraj, accidentally wrote 'replace' instead of 'escape' for some reason; noticed as soon as I posted and had to do a quick ninja edit is the likely reason. Thanks. – seantunwin Feb 04 '20 at 21:22
0

Since you want to replace them all, you should use /\$+/to match 1 or more of them and make sure you escape the $ with \$ to match the literal string "$"

"$$$$$1.00".replace(/\$+/, "")
hawkstrider
  • 3,714
  • 16
  • 26