For example, this string:
" A string example. "
Should be converted into a new version with those leading and trailing spaces removed:
"A string example."
Here's a couple options you can use for removing the trailing/ending spaces from your strings.
Supported by all browsers besides Internet Explorer.
You can also use a regular expression:
This could be used like this:
let string = " A string. " string = string.replace(/^\s+|\s+$/g, "") // "A string."
Supported by all browsers.
You can apply a
replace() method on your string with a regular expression:
string.replace(/^[ ]+|[ ]+$/g, "");
It will remove any leading/trailing spaces from your string.
Here's how you'd use it:
let string = " Your string. " string = string.replace(/^[ ]+|[ ]+$/g, ""); // "Your string."