Given that:
var pattern = "{0}";
Why does this not work:
pattern.replace(/\{0\}/g, "$0.00");
and yet:
pattern.replace("{0}", "$0.00");
the first results in: "{0}.00" the second results in "$0.00"
meanwhile the following does work as expected (producing "$1.00"):
pattern.replace(/\{0\}/g, "$1.00");
Any tips or advice would be really appreciated.
Given that:
var pattern = "{0}";
Why does this not work:
pattern.replace(/\{0\}/g, "$0.00");
and yet:
pattern.replace("{0}", "$0.00");
the first results in: "{0}.00" the second results in "$0.00"
meanwhile the following does work as expected (producing "$1.00"):
pattern.replace(/\{0\}/g, "$1.00");
Any tips or advice would be really appreciated.
Share Improve this question asked Sep 11, 2013 at 20:19 Ryan PosenerRyan Posener 1912 silver badges10 bronze badges 2-
1
don't use
{
use(
– progrenhard Commented Sep 11, 2013 at 20:20 - Actually both examples work fine in Chrome. Which browser are you testing with? – tmh Commented Sep 11, 2013 at 20:23
1 Answer
Reset to default 5In a replacement string with regex, $0
(and $&
) represent the entire match. $1
represents the first subpattern, and so on.
The appropriate workaround is to use $$
, as this will be replaced with a literal $
.
pattern.replace(/\{0\}/g,"$$0.00");