javascript need to do a right trim

Nate Pet picture Nate Pet · Nov 15, 2011 · Viewed 46.2k times · Source

In javascript, how to I do a right trim?

I have the following:

    var s1 = "this is a test~";

     var s = s1.rtrim('~') 

but was not successful

Answer

Rob W picture Rob W · Nov 15, 2011

Use a RegExp. Don't forget to escape special characters.

s1 = s1.replace(/~+$/, ''); //$ marks the end of a string
                            // ~+$ means: all ~ characters at the end of a string