var a = b = " /var/www/site/Brand new document.docx "; console.log( a.split(' ').join('') ); console.log( b.replace( /s/g, '') );