简体   繁体   English

如何替换字符串中的反斜杠美元符号

[英]How to replace backslash dollar sign in a string

I have a text where dollar sign is already escaped.我有一个文本,其中美元符号已经转义。 How to replace \$ with $ ?如何用 $ 替换\$ $

month=\$(date +%m)

becomes变成

month=$(date +%m)

Since backslash and dollar sign both have special meaning in a regexp, you need to escape both of them.由于反斜杠和美元符号在正则表达式中都有特殊含义,因此您需要将它们都转义。

 let str = 'month=\\$(date +%m)'; console.log('before', str); str = str.replace(/\\\$/g, '$'); console.log('after', str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM