Pages

Tuesday, 30 October 2012

Find and replace one by one in jquery /javascript

How to replace string one at a time in jquery / javascript ?

 Below is a simple example explaining that how to replace string one at a time.

Example: Replace "jquery"  with cpas "JQUERY" one by one.

click here for demo

Note* :For  below example jquery is not required and code is compatible with most of the major browsers.

//string  "How to replace string in jquery or javascript one at a time !!!" is repeated twice //in text

var text " How to replace string in jquery or javascript one at a time !!! How to replace string in jquery or javascript one at a time !!! How to replace string in jquery or javascript one at a time !!! ";

var textToReplace "jquery";
var textReplaceWith "JQUERY"
    
var numOfOccur text.match(/jquery/g).length;

//replace content one by one
while(numOfOccur-- >= 0){
    text text.replace(textToReplace ,textReplaceWith );
}
$("#content").html(text );

Check Below links :

Find and Replace all in jquery

No comments:

Post a Comment