简体   繁体   English

JavaScript replace()删除两个特殊字符之一

[英]Javascript replace() removing one of the two special characters

Replacing {t} in the variable var2, removes a '$' symbol from var1 and renders it as abc$abc instead of abc$$abc in the final result. 在变量var2中替换{t},从var1中删除“ $”符号,并将其呈现为abc $ abc而不是最终结果中的abc $$ abc。

var var1 = 'abc$$abc';
var var2 = 'You are comparing {t} entity';
$('#div1').append('<div id="div2">' + var2.replace('{t}','<a href="' + var1 + '" target="_self">' + '</a>') + '</div>');

The closest questions I could find was Javascript append element with special characters , but even that couldn't address the problem. 我能找到的最接近的问题是带有特殊字符的Javascript追加元素 ,但即使这样也无法解决问题。

Undoubtedly, I can check if the resultant string contains just one $ of two $s but i want to know if there is anything wrong with append above, as well as, if there is any generic way to achieve the result with the resultant string being restored with two $ signs. 毫无疑问,我可以检查结果字符串是否仅包含两个$ s中的一个$,但是我想知道上面的append是否有问题,以及是否有任何通用的方法来实现结果字符串用两个$符号恢复。

$$ is a replacement pattern in String.replace() which is always replaced by a single $ . $$String.replace()替换模式 ,始终由单个$替换。 You need to double all dollar signs: 您需要将所有美元符号加倍:

var var1 = 'abc$$abc'.replace( /\$/g, '$$$$' );

(Four dollar signs because again they act as replacement patterns.) (四个美元符号,因为它们再次充当替换模式。)

$ has special meaning in javascript string replace. $在javascript字符串替换中具有特殊含义。 When you use $$ you are basically escaping one of them. 当您使用$$时,基本上可以逃脱其中之一。 If you'd like two you can use four $$$$ 如果您想要两个,可以使用四个$$$$

To make your code work specifically, I think this would be about the most simple thing: 为了使您的代码特别有效,我认为这是最简单的事情:

var var1 = 'abc$$abc'.split("$").join("$$");

JSFiddle JSFiddle

See here for details on $ in replace() : Mozilla docs 有关$ in replace() $详细信息,请参见此处: Mozilla docs


Figured I'd insert my comments here as well: 想通了,我也会在这里插入我的评论:

If you need to make sure this is done during the {t} replace (if the string may change after initialization) you can just throw it in like this: 如果您需要确保在{t}替换期间完成此操作(如果字符串在初始化后可能会更改),则可以像这样将其放入:

$('#div1').append('<div id="div2">' + 
    var2.replace('{t}','<a href="' + 
        var1.split("$").join("$$") + 
        '" target="_self">' + '</a>')
+ '</div>');

or if this is something that needs to be done often in various places... you could create your own function for it... like: 或者,如果这是需要经常在各个地方完成的工作,则可以为其创建自己的函数,例如:

function replaceEscapingDollars(str, find, replacement) { 
    return str.replace(find, replacement.split("$").join("$$"));
}

then your code would look like: 那么您的代码将如下所示:

$('#div1').append('<div id="div2">' + 
    replaceEscapingDollars(var2, '{t}', '<a href="' + var1 + '" target="_self">' + '</a>') +
'</div>');

JSFiddle JSFiddle


But the simplest thing of all might be just to not use replace at all... 但最简单的事情可能就是根本不使用replace ...

var2.split('{t}').join(var1)

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM