Skip to content

Instantly share code, notes, and snippets.

@Victa
Created January 5, 2013 22:40

Revisions

  1. Victa created this gist Jan 5, 2013.
    17 changes: 17 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // 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"