I see the trim 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 ltrim and rtrim?
You can use a simple regex replace
string.replace(/\s+$/, '')
Demo: Fiddle
function rtrim(str){
return str.replace(/\s+$/, '');
}
jQuery doesn't have the methods ltrim or rtrim.
You can do it easily:
function rtrim(str){
return str.replace(/\s+$/, "");
}
function ltrim(str){
return str.replace(/^\s+/, "");
}
I don't suggest you add a method on the String prototype.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With