简体   繁体   中英

Match Escaped Dollar Sign in literal but not variable

I'm trying to determine if a value matches a string input by the user. When trying to match a dollar sign, I take the input string and replace the dollar sign with an escaped dollar sign ("\\\\$"). However, this doesn't match, but it will match if instead of assigning this value to a variable I just use the literal string. Here's a console snapshot illustrating this:

在此处输入图片说明

why is this happening??

That's not how escaping works exactly, consider:

在此处输入图片说明

Your b.toString() !== "\\\\$"

Instead of "$".replace("$", "\\\\\\\\$") you want "$".replace("$", "\\\\$")

在此处输入图片说明

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