开发者

how to replace the text with desired text in a para

开发者 https://www.devze.com 2023-01-31 06:11 出处:网络
I have the following code.. in this I am trying to find the particular text and replace with desired one.. this is working fine if the string is in same case.. if we give withcapitals (like.. the as T

I have the following code.. in this I am trying to find the particular text and replace with desired one.. this is working fine if the string is in same case.. if we give with capitals (like.. the as The) it is not giving out put.. so what is the solution for this.. can we use regular expressions in this one..? please help me

<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>  <head>   <title> New Document
</title> <script
type="text/javascript"
src="jquery-1.4.2.min.js"></script>
<script> $(document).ready(function(){
$('#replace').click(function(){ var
oldstr=$('#inputstring').val(); var
newstr=$('#newstring').val(); var
para=$('#para').html();

var x=para.replace(oldstr,newstr);
$('#para').empty().html(x);

}) }) </script>  </head>

<bo开发者_JAVA百科dy> Enter your string here:<input
type="text" id="inputstring"><br>
Enter new string here:<input
type="text" id="newstring"><br> <input
type="button" id="replace"
value="Replace"><br>

<p id="para">This is the new paragraph
written to test how to replace the a
string with desired string</p> 
</body> </html>


You need to use ignorecase flag for the regular expression.

para.replace(new RegExp(oldstr, 'i'), newstr

Docs: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

0

精彩评论

暂无评论...
验证码 换一张
取 消