This will
/ +$/
Space character ( ) at least once ( + ), at the end of the line ( $ )
Note that placing a space in a character class is not required if you do not want to match more than a space character, such as the [\t ] tabs.
To really match only one last space, use / $/
EDIT : cut everything after the last space (thinking about it, this is what you really think is necessary, you can use:
regex.replace(/ +\S*$/ig, '');
where the regular expression means: "At least one space and any characters without spaces after it ( \S* ), at the end of the line ( $ ).".
This can only match the last bit of the line after the last space. As a side effect, the string is trimmed at the end.
Tomalak
source share