开发者

how to replace the text with desired text in a para

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜