简体   繁体   English

用另一个jQuery替换字符串的所有实例

[英]Replace all instances of a string with another jquery

I am trying to replace all instances of a recurring string within a specific div with another using coffee script. 我正在尝试使用coffee脚本将特定div中的重复字符串的所有实例替换为另一个。 I am not entirely sure how to do this. 我不太确定该怎么做。 my HTML is: 我的HTML是:

<div id="post_delete_template" style="display:none">
    <tr>
        <td>
            <div>
                <form data-post-id="<%= current_user.id %>" data-post-id="_id_" id="post_delete__id_" action="<%= url_for post_delete_path('_id_') %>" method="DELETE"><a href="#" id="delete_post_button">Delete</a></form>
            </div>
        </td>
    </tr>
 </div>
<br />

Question is, in jquery(preferably coffee script notation) how do I replace all ' _id_ ' instances in my div block with lets say '99'? 问题是,在jquery(最好是咖啡脚本表示法)中,我该如何用“ 99”代替div块中的所有“ _id_”实例?

if you want just a string replace with javascript with your example try this 如果您只想将字符串替换为javascript,请尝试以下示例

var str1 = "abcdef_id_test";
var str2 = str1.replace(/_id_/g, '99');
alert(str2);// show abcdef99test

demo 演示

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

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