简体   繁体   中英

Replace word with string using regular expression

I have some string and in string i wana to replace 355#00$1 with 7777777 but it's not working... what im doing wrong please help..

<p class="str_content">
you mean .active class can be any of the 6 li's and you355#00$1 ne355#00$1ed to find the link of that li355#00$1's anchor in other navs and assign .active class to them.Am I right?
prateek
you mean .active class can be any of the 6 li's355#00$1 and you need to find the link of that li's355#00$1 anchor in other navs and assign .active class to them.Am I right?
prateek
you mean .active355#00$1 class can be any of the 6 li's and you need to find the link of that li's anchor in355#00$1 other navs and assign .active class to them.Am I right?
355#00$1

</p>

<p class="replace">Replace</p>

<script>
$(".replace").on("click",function(e){
    var data=$(".str_content").html();  
    var new_data=data.replace(/'355#00$1'/gi,'7777777');
    $(".str_content").html(new_data);

    alert($(".str_content").html(new_data));

});
</script>

Your code is almost correct, the issue is with the syntax of the regular expression. You don't need to place it in quotes, and the $ character needs to be escaped as it has a special meaning in Regex.

Also note that you can provide html() with a function to amend the value instead of selecting the element multiple times. It may also be worth using text() instead, as you don't appear to have any HTML within the content of the p element. Try this:

 $(".replace").on("click", function(e) { $(".str_content").html(function(i, html) { return html.replace(/355#00\\$1/g, '7777777'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="str_content"> you mean .active class can be any of the 6 li's and you355#00$1 ne355#00$1ed to find the link of that li355#00$1's anchor in other navs and assign .active class to them.Am I right? prateek you mean .active class can be any of the 6 li's355#00$1 and you need to find the link of that li's355#00$1 anchor in other navs and assign .active class to them.Am I right? prateek you mean .active355#00$1 class can be any of the 6 li's and you need to find the link of that li's anchor in355#00$1 other navs and assign .active class to them.Am I right? 355#00$1 </p> <p class="replace">Replace</p> 

Use replace like,

var new_data=data.replace(/355#00\$1/ig,'7777777');

 $(".replace").on("click",function(e){ var data=$(".str_content").html(); var new_data=data.replace(/355#00\\$1/ig,'7777777'); $(".str_content").html(new_data); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <p class="str_content"> you mean .active class can be any of the 6 li's and you355#00$1 ne355#00$1ed to find the link of that li355#00$1's anchor in other navs and assign .active class to them.Am I right? prateek you mean .active class can be any of the 6 li's355#00$1 and you need to find the link of that li's355#00$1 anchor in other navs and assign .active class to them.Am I right? prateek you mean .active355#00$1 class can be any of the 6 li's and you need to find the link of that li's anchor in355#00$1 other navs and assign .active class to them.Am I right? 355#00$1 </p> <p class="replace">Replace</p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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