[英]string with "$" not giving expected output in Javascript
I have a string "$$$$$1.00"
Need to replace all '$' with empty string.我有一个字符串
"$$$$$1.00"
需要用空字符串替换所有的 '$'。 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我已经为简单的可执行文件JSFiddle添加了 JSfiddle 链接
You need to escape the dollar sign ( $
) in the RegExp as this means the end of the string in RegExp world.您需要对 RegExp 中的美元符号 (
$
) 进行转义,因为这意味着 RegExp 世界中字符串的结尾。
Refactor to:重构为:
"$$$$$1.00".replace(/\$/g, "")
既然你要全部更换,你应该使用/\\$+/
匹配1或更多的人,并确保你逃避$
用\\$
以文本字符串“$”匹配
"$$$$$1.00".replace(/\$+/, "")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.