2017-04-13 3 views
1

ich eine Schnur habe und in String i wana 355 # $ 00 gegen 1 7777777 zu ersetzen, aber es funktioniert nicht ... was im falsch zu machen bitte helfen ..Wort mit Zeichenfolge mit regulärem Ausdruck

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

Antwort

4

Ihr Code ist fast c Das Problem tritt mit der Syntax des regulären Ausdrucks auf. Sie müssen es nicht in Anführungszeichen setzen, und das Zeichen $ muss escaped sein, da es in Regex eine besondere Bedeutung hat.

Beachten Sie auch, dass Sie html() mit einer Funktion zur Verfügung stellen können, um den Wert zu ändern, anstatt das Element mehrmals auszuwählen. Es kann sich auch lohnen, stattdessen text() zu verwenden, da Sie anscheinend keinen HTML-Code innerhalb des Inhalts des Elements haben. Versuchen Sie folgendes:

$(".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>

1

Verwenden ersetzen wie

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>

Verwandte Themen