When you need to remove all spaces at the end:
str.replace(/\s*$/,'');
When you need to delete one place at the end:
str.replace(/\s?$/,'');
\s means not only space, but also space-like characters; e.g. tab.
If you are using jQuery, you can also use the trim function:
str = $.trim(str);
But trim removes spaces not only at the end of a line, but also at the beginning.
source share