Code to trim the string using JavaScript using Regular Expression:
- <!DOCTYPE html>
- <html>
-
- <body>
- <button onclick="myFunction()">Trim the string</button>
- <script>
- function myTrim(strObj)
- {
- return strObj.replace(/^\s+|\s+$/gm, '');
- }
-
- function myFunction()
- {
- var tempStr = myTrim(" Welcome to RK JS Code! ");
- alert(tempStr);
- }
- </script>
- </body>
-
- </html>