Trim the String in JavaScript using Regular Expression

Code to trim the string using JavaScript using Regular Expression:
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.     <body>  
  5.         <button onclick="myFunction()">Trim the string</button>  
  6.         <script>  
  7.         function myTrim(strObj)  
  8.         {  
  9.             return strObj.replace(/^\s+|\s+$/gm, '');  
  10.         }  
  11.   
  12.         function myFunction()  
  13.         {  
  14.             var tempStr = myTrim(" Welcome to RK JS Code! ");  
  15.             alert(tempStr);  
  16.         }  
  17.         </script>  
  18.     </body>  
  19.   
  20. </html>