Skip to content

Instantly share code, notes, and snippets.

@camt
Created January 22, 2012 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camt/1656240 to your computer and use it in GitHub Desktop.
Save camt/1656240 to your computer and use it in GitHub Desktop.
trimming white space in JS easily
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