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.
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