Computer >> Computer tutorials >  >> Programming >> Javascript

How to replace all occurrences of a string in JavaScript?


To replace occurrences of a string in JavaScript, use the replace() method −

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