Pre: I have my own library, where I work with namespaced modules and classes in 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.
Now i struggled doing it reverse: replace(/::/g,'$$')
results in Module$ClassName
having only one Dollar ($)
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?
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
. See 'Specifying a string as a parameter' in https://developer.mozilla.org/en-US/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. 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.
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.