Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using jquery and now I need to remove spaces in the string retrieved as a parameter (file name).. For instance:

I have: '/var/www/site/Brand new document.docx'
I would like to get: '/var/www/site/Brandnewdocument.docx'

Someone knows how to solve this? I tryied replace method but it didn't work :(

Thank you guys.

share|improve this question

2 Answers

up vote 27 down vote accepted

This?

str = str.replace(/\s/g, '');

Live demo: http://jsfiddle.net/Rkb57/


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.

share|improve this answer
just beat me... – Gaurav May 11 '11 at 11:06
@Gaurav I've looked up similar answers on SO, and I see .replace(/\s+/g, '') more often. Is there a difference between that and my answer? – Šime Vidas May 11 '11 at 11:17
in this case there is no difference. But + is used for finding with atleast one occurrence. – Gaurav May 11 '11 at 11:23
It worked flawless! Thanks – Mobster May 11 '11 at 12:27
2  
@Mobster It worked flawless ly :) – Šime Vidas May 11 '11 at 12:31
var a = "/var/www/site/Brand new document.docx";
alert(a.split(' ').join(''));
alert(a.replace( /\s/g, "")); 

Two ways of doing this!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.