Example 1: Trim a String
Output
Hello World
In the above example, the trim() method is used to trim a string.
The trim() method removes white space from both sides of the string.
Example 2: Trim a String Using RegEx
Output
Hello World
In the above program, the RegEx is used with the replace() method to trim the string.
/^\s+|\s+$/g checks for whitespace at the beginning and end of the string.