3

次のようなテキストを置き換えようとしています。

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.

私がここに欠けているものはありますか?

4

1 に答える 1

4

$結果に1つの結果の符号$$を取得する必要があるため、置換の特殊文字です。$必要なものを取得するには、追加の$文字が必要になります。詳細については、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.
于 2013-03-11T23:47:25.480 に答える