JavaScript replace()方法美元符号 [英] JavaScript replace() method dollar signs

查看:583
本文介绍了JavaScript replace()方法美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如 aman / gupta ,我想将其替换为 aman $$ gupta 并且我使用JavaScript 替换方法如下:

I have a string like aman/gupta and I want to replace it to aman$$gupta and for that I am using JavaScript replace method as follows:

a = "aman/gupta"
a = a.replace("/", "$")
// 'aman$gupta'

a = "aman/gupta"
a = a.replace("/", "$$")
// 'aman$gupta'

a = "aman/gupta"
a = a.replace("/", "$$$")
// 'aman$$gupta'

为什么是第一和第二种情况相同,当我使用 $$$ 而不是 $$ 时,我得到了预期的结果?

Why are the 1st and 2nd case identical and I get the expected result when I use $$$ instead of $$?

推荐答案

这是因为 $$ 插入 $

因此,您需要使用:

a = "aman/gupta";
a = a.replace("/", "$$$$"); // "aman$$gupta"






查看以下特殊模式

Pattern   Inserts
$$        Inserts a "$".
$&        Inserts 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 is a non-negative integer lesser than 100, inserts the nth
            parenthesized submatch string, provided the first argument was a
            RegExp object.

这篇关于JavaScript replace()方法美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆