Javascript: remove trailing spaces only
Update 2019
String trim methods have been added to ECMAScript spec in 2019 with wide browser support
String.trim()
String.trimStart()
String.trimEnd()
You can use like this:
" test ".trimEnd() // " test"
Further Reading
- tc39 proposal -
String.prototype.{trimStart,trimEnd}
" test ".replace(/\s+$/g, '');
Use String#replace
with regex /\s+$/
and replacing text as empty string.
string.replace(/\s+$/, '')
console.log(
'-----' + ' test '.replace(/\s+$/, '') + '-----'
)