javascript need to do a right trim
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 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 You can modify the String prototype if you like. Modifying the String prototype is generally frowned upon, but I personally prefer this method, as it makes the code cleaner IMHO. String.prototype.rtrim = function(s) { return this.replace(new RegExp(s + "*$"),''); }; Then call... var s1 = "this