Skip to content

Instantly share code, notes, and snippets.

@Victa
Created January 5, 2013 22:40
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 Victa/4464100 to your computer and use it in GitHub Desktop.
Save Victa/4464100 to your computer and use it in GitHub Desktop.
Strip Whitespace From String
// Remove leading and trailing whitespace
// Requires jQuery
var str = " a b c d e f g ";
var newStr = $.trim(str);
// "a b c d e f g"
// Remove leading and trailing whitespace
// JavaScript RegEx
var str = " a b c d e f g ";
var newStr = str.replace(/(^\s+|\s+$)/g,'');
// "a b c d e f g"
// Remove all whitespace
// JavaScript RegEx
var str = " a b c d e f g ";
var newStr = str.replace(/\s+/g, '');
// "abcdefg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment