简体   繁体   中英

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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