admin管理员组文章数量:1415140
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");
本文标签: javascriptstring regex patternreplace(0quot000quot) does not workStack Overflow
版权声明:本文标题:javascript - string regex pattern.replace({0}, "$0.00") does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745228935a2648737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论