简体   繁体   English

如果我想用“ $$”(两美元)作为Java- / coffescript-中的替换字符串,为什么我需要“ $$$”(三美元)

[英]Why do I need “$$$” (three dollar) if I want “$$” (two dollar) as replace string in Java- / coffescript-

Pre: I have my own library, where I work with namespaced modules and classes in Coffescript. 上一页:我有自己的库,在其中使用Coffescript中的命名空间模块和类。 this works like in Ruby "namspace::module::class", since '::' is not allowed as class name I replace Module::ClassName with Module$$ClassName -> replace(/\\$\\$/g,'::') , fine. 这就像在Ruby“ namspace :: module :: class”中一样,因为不允许使用“ ::”作为类名,所以我将Module::ClassName替换为Module$$ClassName > replace(/\\$\\$/g,'::') ,很好。

Now i struggled doing it reverse: replace(/::/g,'$$') results in Module$ClassName having only one Dollar ($) 现在我很努力地做相反的事情: replace(/::/g,'$$')导致Module$ClassName只有一个美元($)

So i played a around a bit 所以我打了一下

a="a:a::b:b::c:c"
a.replace(':','$')         #-> "a$a::b:b::c:c"  clear only first 
a.replace(/:/g,'$')        #-> "a$a$$b$b$$c$c"  better, but wrong we want '::' only 
a.replace(/::/g,'$$')      #-> "a:a$b:b$c:c"    suprise; where is the 2nd Dollar? 
a.replace("::",'$$')       #-> "a:a$b:b::c:c"   try no regexp since dollar has an other meaning? fails only one $
a.replace(/::/g,'\$\$')    #-> "a:a$b:b$c:c"    ESC the guys? nope  
a.replace(/::/g,"\\$\\$")  #-> "a:a\$\$b:b\$\$c:c" ESC ESC to get into the deeper?
                           #   and then replace(/\\\$/g,'$') ? overkill  
a.replace(/::/g,'$$$')     #-> "a:a$$b:b$$c:c"  bingo, but why? 
# trying more
a.replace(/::/g,'$$$$')    #-> "a:a$$b:b$$c:c"  2 get one? one stays alone 
a.replace(/::/g,'$$$$$')   #-> "a:a$$$b:b$$$c:c"  seems so

After all, is logic (and I wonder why I never had the prob before). 毕竟是逻辑(而且我想知道为什么我以前从未有过这个问题)。

So I think (am sure) that '$$' escapes to one '$' because '$n' references to matching groups - but even if there is no regexp in? 因此,我认为(肯定)“ $$”转为一个“ $”,因为“ $ n”引用了匹配的组- 即使没有正则表达式?

Even if you don't have any capture groups, $ can be used in the replacement string. 即使您没有任何捕获组,也可以在替换字符串中使用$ $& refers to everything that was matched by the regexp, $` refers to everything before the match, and $' refers to everything after the match. $&表示正则表达式匹配的所有内容, $`表示匹配前的所有内容,而$'表示匹配后的所有内容。 So $ is always treated specially, and $$ means just a single $ . 因此,始终会对$进行特殊处理,而$$只是一个$

The reason is because $ is a special character in the second parameter of replace . 原因是$是replace的第二个参数中的特殊字符。 See 'Specifying a string as a parameter' in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 请参阅https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace中的 “将字符串指定为参数”

Why do I need “$$$” (three dollar) if I want “$$” (two dollar) as replace string 如果我想要“ $$”(两美元)作为替换字符串,为什么我需要“ $$$”(三美元)

In fact, you need 4 $ in the replacement string. 实际上,您在替换字符串中需要4 $ To replace with a literal $ , you need two $ s, as one $ "escapes" the other. 要用文字$替换,您需要两个$ ,因为一个$ “逸出”另一个。

Thus, you need 因此,您需要

 var a = "some::var"; a = a.replace(/::/g,'$$$$'); // this will replace `::` with `$$` document.body.innerHTML = a; 

If you add an odd $ , it will be treated as a literal, or omitted, or whatever a specific browser wants to do with this "wild" escaping symbol. 如果添加一个奇数$ ,它将被视为文字,或被忽略,或特定浏览器想要与此“通配”转义符号进行的任何操作。 Thus, it is safer to use an even number of $ s in the replacement pattern. 因此,在替换模式中使用偶数的$ s是更安全的。

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

相关问题 我需要在javascript变量名之前加一美元吗? - Do I need to put a dollar before variable names with javascript? 如何在javascript / jquery中添加字符串美元金额? - How do i add a string dollar amount in javascript/jquery? 为什么我在JavaScript中的某些构造函数之前看到一个美元符号? - Why do I see a dollar sign before some constructors in JavaScript? 如何在Javascript中添加美元金额 - How do I add to dollar amounts in Javascript 如何在javascript中用下划线(_)替换所有出现的美元($)? - How can I replace all occurrences of a dollar ($) with an underscore (_) in javascript? 如何替换字符串中的反斜杠美元符号 - How to replace backslash dollar sign in a string 我如何返回这个 function 并在它前面加上一个美元符号。 (Javascript) - How do I return this function with a dollar sign before it. (Javascript) 如何将包含美元符号“$”的两个值相加? 我一直试图找出方法和属性,仍然没有线索 - How do you sum two values containing the dollar sign “$”? I have been trying to figure out the methods and properties, still no clue JavaScript字符串中的美元符号 - Dollar sign in Javascript string 如何清理/替换无法转义的 JS 字符串中的 $&(美元 + 号)? - How to sanitize/replace $& (dollar + ampersand) in a JS string that cannot be escaped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM