如何替换JavaScript中所有出现的字符串?

要替换JavaScript中出现的字符串,请使用replace()方法-

<html>
   <body>
      <script>
         var str1 = 'John loves studying. He loves Football too!';
         var str2 = str1.replace(/loves/g, "likes");
         document.write(str2);
      </script>
   </body>
</html>