Fernando Paladini Fernando Paladini - 8 months ago 45
Javascript Question

Trim blank spaces from the right in jQuery

I see the

method in jQuery, but I can't find, in its documentation, how to trim blank spaces from only the right of the string.

As an example, if I have:

"foobar " "foo " " foo bar "

Trimming the spaces from the right of the string results in:

"foobar" "foo" " foo bar"

Is there any method in jQuery to trim the spaces from the right? I mean, something like


You can use a simple regex replace

string.replace(/\s+$/, '')

Demo: Fiddle

function rtrim(str){
    return str.replace(/\s+$/, '');