If you want to remove space at begin and end of a string, use the below
var str = " Hello World! ";
var trim = str.trim()
If you want to remove space only at the end of a string, use the below
var str = " Hello World! ";
var trim = str.replace(/(\s+$)/g, "")
Hope this helps you.