Trim,ltrim & rtrim in #javascript
Simple class to trim,ltrim and rtrim as it’s common in other languages.
trim = { both : function(str,ch) { # Equivalent to trim return trim.lead(trim.trail(str,ch),ch); }, lead : function(str,ch) { # Equivalent to ltrim if (!ch) ch="\\s" return str.replace( new RegExp("^["+ch+"]+", "g"), "" ); }, trail : function(str,ch) { # Equivalent to rtrim if (!ch) ch="\\s"; return str.replace( new RegExp("["+ch+"]+$", "g"), "" ); } }You can use it like this
s = " string "; # Trim both left and right, every type of char alert(trim.both(s)); # Trim only right, and just look for \n alert(trim.trail(s,"\\n"));