[英]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.