简体   繁体   English

匹配文字中的转义美元符号,但不可变

[英]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() !== "\\\\$" 您的b.toString() !== "\\\\$"

Instead of "$".replace("$", "\\\\\\\\$") you want "$".replace("$", "\\\\$") 而不是"$".replace("$", "\\\\\\\\$")而是要"$".replace("$", "\\\\$")

在此处输入图片说明

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

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