Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Replace All Occurrences a String</title> </head> <body> <h4>Original String</h4> <p>freedom is not worth having if it does not include the freedom to make mistakes.</p> <h4>String After Replacement</h4> <script> var myStr = 'freedom is not worth having if it does not include the freedom to make mistakes.'; var newStr = myStr.replace(/freedom/g, "liberty"); // Printing the modified string document.write('<p>' + newStr + '</p>'); </script> </body> </html>