Posted By: Anonymous
How to remove spaces in a string? For instance:
'/var/www/site/Brand new document.docx'
str = str.replace(/s/g, '');
var str = '/var/www/site/Brand new document.docx'; document.write( str.replace(/s/g, '') );
Update: Based on this question, this:
str = str.replace(/s+/g, '');
is a better solution. It produces the same result, but it does it faster.
s is the regex for “whitespace”, and
g is the “global” flag, meaning match ALL
A great explanation for
+ can be found here.
As a side note, you could replace the content between the single quotes to anything you want, so you can replace whitespace with any other string.