How to write a Regular Expression in JavaScript to remove spaces?

JavascriptWeb DevelopmentFront End Technology

To remove spaces in JavaScript, you can try to run the following regular expression. It removes the spaces from a string −

Example

 Live Demo

<html>
   <head>
      <script>
         var str = "Welcome to Tutorialspoint";
         document.write(str);
         
         //Removing Spaces
         document.write("<br>"+str.replace(/\s/g, ''));
      </script>
   </head>
   
   <body>
   </body>
</html>

Output

raja
Published on 23-Feb-2018 06:41:53
Advertisements