Is there an alternative to the
The method should remove the spaces from the beginning/end of the string and then return the new string.
This regular expression will do the trick for you:
string.replace(/^[ ]+|[ ]+$/g, '');
It will remove any leading/trailing spaces from your string.
Here's how you'd use it:
let string = " My example string. " string = string.replace(/^[ ]+|[ ]+$/g, ''); // "My example string."
You can use a regular expression with a
replace() string method:
This will take any leading and/or trailing spaces that may exist and removes them.
All browsers will support this.