Created
January 22, 2012 08:19
-
-
Save camt/1656240 to your computer and use it in GitHub Desktop.
trimming white space in JS easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trim(str, chars) { | |
return ltrim(rtrim(str, chars), chars); | |
} | |
function ltrim(str, chars) { | |
chars = chars || "\\s"; | |
return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); | |
} | |
function rtrim(str, chars) { | |
chars = chars || "\\s"; | |
return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment