How to replace all occurrences of a string in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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>


raja
Published on 17-Jan-2018 11:40:55
Advertisements