JavaScript Replace All in String

Replace All in a String using JavaScript, jQuery


This below snippet will help to replace a all occurrence of a word in string using the below example. This method is suitable for JavaScript and jQuery.
To replace first occurrence we can use

var main_str="Find Find Replace";
main_str=main_str.replace("Find","Replace");
alert(main_str);//Will display "Replace Find Replace"

To replace all occurrence in a String using the below code

var main_str="Find Find Replace";
main_str=main_str.replace(/Find/g,"Replace");
alert(main_str);//Will display "Replace Replace Replace"


Related Topic Javascript Spell Checker Embedded Code, Text Area Object
Removing special characters in a string using javascript
Remove HTML Tags in javascript
Remove Single Double Quotes From String Javascript
JavaScript Date Formatting to String mm dd yyyy hh mm ss zz pp

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback