我正在尝试替换一些文本,例如:
I need the %r for the bike.
Where%r被另一个值替换。
可以说我替换%r为$$$$.
var text = 'I need the %r for the bike.';
return text.replace("%r", "$$$$");
而不是得到预期的结果:
I need the $$$$ for the bike.
我得到:
I need the $$ for the bike.
我在这里缺少什么吗?
我正在尝试替换一些文本,例如:
I need the %r for the bike.
Where%r被另一个值替换。
可以说我替换%r为$$$$.
var text = 'I need the %r for the bike.';
return text.replace("%r", "$$$$");
而不是得到预期的结果:
I need the $$$$ for the bike.
我得到:
I need the $$ for the bike.
我在这里缺少什么吗?
$是替换中的特殊字符,因为您需要在结果$$中获得一个结果$符号。你需要额外的$字符才能得到你想要的。有关详细信息,请参阅MDN 参考.replace()。
替换字符串中的各种特殊序列是:
$$ - Inserts a "$"
$& - Insert the matched substring
$` - Inserts the portion of the string that precedes the matched substring
$' - Inserts the portion of the string that follows the matched substring
$n - Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.