Create your own trim() with replace() and regex to remove empty space from string - Javascript RegExp

Javascript examples for RegExp:Match Space

Description

Create your own trim() with replace() and regex to remove empty space from string

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from www. j a  v a2 s . c  o m*/
function myTrim(x) {
       return x.replace(/^\s+|\s+$/gm,'');
    }
    function myFunction() {
       var str = myTrim("        Hello World!        ");
       console.log(str);
    }
myFunction();
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials