Replace all occurrences of a string using javascript

In JavaScript we use “replace” function to replace a particular word or string.

For Example,

var str = "Replace text in javascript using replace function of javascript";
str = str.replace('javascript','jscript');

here, it will only replace the first occurrence of the ‘javascript’ in string.
Output: “Replace text in jscript using replace function of javascript”

So, to replace all occurrences use

str  = str.replace(/javascript/g, 'jscript');
OR
str  = str.replace(new RegExp('javascript','g') , 'jscript');