让IE用文字字符串“$ 0”替换正则表达式

时间:2021-01-05 16:52:34

I'm replacing a bunch of values in a regex with dollar amounts. The problem is that in IE, if I attempt to replace with an amount of $0 (or, theoretically, $1--$9), the replace never occurs since the it's trying to replace a non-existant group:

我正在使用美元金额替换正则表达式中的一堆值。问题是在IE中,如果我尝试用$ 0(或理论上,$ 1 - $ 9)的金额替换,则替换永远不会发生,因为它正在尝试替换不存在的组:

'foo'.replace(/f/g, '$0');

I want the result of that replacement to be "$0oo", which it is in Chrome & FF, but not IE9 (haven't tried other versions of IE).

我希望替换的结果是“$ 0oo”,它在Chrome&FF中,但不是IE9(没有尝试过其他版本的IE)。

Chrome & FF have an "issue" with the following, in that I can't figure out how to replace with a literal $1:

Chrome和FF有以下“问题”,因为我无法弄清楚如何用文字$ 1替换:

'foo'.replace(/(f)/g, '$1')

4 个解决方案

#1


8  

Write "$$0" to escape the $ sign.

写“$$ 0”以逃避$符号。

#2


4  

In Chrome and Firefox you can workaround this limitation by using a function as the second argument of the replace function:

在Chrome和Firefox中,您可以使用函数作为替换函数的第二个参数来解决此限制:

'foo'.replace(/(f)/g, function(){return '$1';}); // => $1oo

My own testing shows that IE8 is consistent with the other browsers on this issue.

我自己的测试显示IE8在这个问题上与其他浏览器一致。

#3


2  

The Problem:

'My Bank balance is {0} '.replace(/\{0\}/,'$0.22')
 results: "My Bank balance is {0}.22 " 
 This is not what we want

The solution:

'My Bank balance is {0} '.replace(/\{0\}/,'$$0.22')
results: "My Bank balance is $0.22 " 
'My Bank balance is {0} '.replace(/\{0\}/,"$0.22".replace(/\$/,'$$$$'))
result: "My Bank balance is $0.22 "

Explanation: I am trying to find '$' and replace by '$$'. To match '$' you have to escape it by '\$' and to replace '$$$$' you have to put four '$' as RegEx escape '$' for '$$'. Fun!!

说明:我试图找到'$'并用'$$'替换。要匹配'$',您必须通过'\''来逃避它,并且要替换'$$$$',您必须将四个'$'作为RegEx转义'$'换取'$$'。开心!

#4


1  

@SLaks that should be more like: write "$$" for literal "$".

@SLaks应该更像:为文字“$”写“$$”。

@cwolves getting a literal instead of a 'special' character (like "$" here) usually involves either doubling it or prefixing it with "\".

@cwolves得到一个文字而不是一个'特殊'字符(比如这里的“$”)通常涉及加倍或用“\”作为前缀。

#1


8  

Write "$$0" to escape the $ sign.

写“$$ 0”以逃避$符号。

#2


4  

In Chrome and Firefox you can workaround this limitation by using a function as the second argument of the replace function:

在Chrome和Firefox中,您可以使用函数作为替换函数的第二个参数来解决此限制:

'foo'.replace(/(f)/g, function(){return '$1';}); // => $1oo

My own testing shows that IE8 is consistent with the other browsers on this issue.

我自己的测试显示IE8在这个问题上与其他浏览器一致。

#3


2  

The Problem:

'My Bank balance is {0} '.replace(/\{0\}/,'$0.22')
 results: "My Bank balance is {0}.22 " 
 This is not what we want

The solution:

'My Bank balance is {0} '.replace(/\{0\}/,'$$0.22')
results: "My Bank balance is $0.22 " 
'My Bank balance is {0} '.replace(/\{0\}/,"$0.22".replace(/\$/,'$$$$'))
result: "My Bank balance is $0.22 "

Explanation: I am trying to find '$' and replace by '$$'. To match '$' you have to escape it by '\$' and to replace '$$$$' you have to put four '$' as RegEx escape '$' for '$$'. Fun!!

说明:我试图找到'$'并用'$$'替换。要匹配'$',您必须通过'\''来逃避它,并且要替换'$$$$',您必须将四个'$'作为RegEx转义'$'换取'$$'。开心!

#4


1  

@SLaks that should be more like: write "$$" for literal "$".

@SLaks应该更像:为文字“$”写“$$”。

@cwolves getting a literal instead of a 'special' character (like "$" here) usually involves either doubling it or prefixing it with "\".

@cwolves得到一个文字而不是一个'特殊'字符(比如这里的“$”)通常涉及加倍或用“\”作为前缀。