Spazi vuoti, ovvero tabulazioni e spazi.
Vanilla JavaScript (Trim Leading e Trailing)
var str = " a b c d e f g "; var newStr = str.trim(); // "a b c d e f g"
Questo metodo è ES 5, quindi nel caso in cui tu possa riempirlo con il polyfill (IE 8 e giù):
if (!String.prototype.trim) ( String.prototype.trim = function () ( return this.replace(/^\s+|\s+$/g, ''); ); )
jQuery (Trim Leading e Trailing)
Se stai comunque usando jQuery:
var str = " a b c d e f g "; var newStr = $.trim(str); // "a b c d e f g"
Vanilla JavaScript RegEx (Trim Leading and Trailing)
var str = " a b c d e f g "; var newStr = str.replace(/(^\s+|\s+$)/g,''); // "a b c d e f g"
Vanilla JavaScript RegEx (Taglia TUTTI gli spazi)
var str = " a b c d e f g "; var newStr = str.replace(/\s+/g, ''); // "abcdefg"
Demo
Vedere Pen Remove Whitespace from Strings di Chris Coyier (@chriscoyier) su CodePen.
Nota che niente di tutto questo funziona con altri tipi di spazi bianchi, ad esempio (spazio sottile) o (spazio unificatore).
Puoi anche tagliare le corde dalla parte anteriore o posteriore.