使用正则表达式将单词替换为字符串

时间:2017-04-13 08:22:04

标签: javascript jquery html

我有一些字符串和字符串我wana用7777777替换355#00 $ 1但是它不起作用...我做错了请帮助..

<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>

2 个答案:

答案 0 :(得分:4)

您的代码几乎是正确的,问题在于正则表达式的语法。您不需要将其放在引号中,$字符需要转义,因为它在Regex中具有特殊含义。

另请注意,您可以为html()提供修改值的功能,而不是多次选择元素。也可能值得使用text(),因为您似乎在p元素的内容中没有任何HTML。试试这个:

&#13;
&#13;
$(".replace").on("click", function(e) {
  $(".str_content").html(function(i, html) {
   return html.replace(/355#00\$1/g, '7777777');
  });
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用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>