How to replace all occurrence of a string in jQuery or Javascript

This is a surprise problem with JavaScript replace() function that is replace only first occurrence of string. But many times we want to replace a particular occurrences of whole string at once. So, now i will told you a simple method to replace all occurrence of a string with online code in jQuery and JavaScript.

How to replace all occurrence of a string in jQuery or Javascript



var str = "Javascript and jQUery is my life"; //example string
str = str.replace(/\ /g, '-');  // this code will replace all spaces with -
alert(str);
The above code will return a string like
Javascript-and-jQUery-is-my-life
How to replace all occurrence of a string in jQuery or Javascript How to replace all occurrence of a string in jQuery or Javascript Reviewed by Awais on 12:06 Rating: 5