469,327 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

remove extra spaces with regexp

I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from the
middle?

Where

"hello world"

becomes

"hello world"

Thanks.
Aug 1 '06 #1
7 16200
Bosconian wrote on 01 aug 2006 in comp.lang.javascript:
I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from
the middle?
[white space \s is more than spaces]
Where

"hello world"

becomes

"hello world"
result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 1 '06 #2
Bosconian wrote:
I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from the
middle?

Where

"hello world"

becomes

"hello world"
But how are you defining an "extra space"?

Probably you would want to replace and continuous sequence of two or
more spaces with a single space.

Richard.

Aug 1 '06 #3
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
Bosconian wrote on 01 aug 2006 in comp.lang.javascript:
>I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from
the middle?

[white space \s is more than spaces]
>Where

"hello world"

becomes

"hello world"

result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan, that's it--thanks! I just learned something new.
Aug 1 '06 #4
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Bosconian wrote:
>I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from
the
middle?

Where

"hello world"

becomes

"hello world"

But how are you defining an "extra space"?

Probably you would want to replace and continuous sequence of two or
more spaces with a single space.

Richard.
Yes, as you described. The syntax that Evertjan provided suits my needs
exactly.
Aug 1 '06 #5
JRS: In article <Xn********************@194.109.133.242>, dated Tue, 1
Aug 2006 17:40:49 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.netposted :
>Bosconian wrote on 01 aug 2006 in comp.lang.javascript:
>I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing extra spaces from
the middle?

[white space \s is more than spaces]
>Where

"hello world"

becomes

"hello world"

result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
That compresses all multiple whitespace, including newlines. Within a
RegExp, a space is a perfectly good character, standing for itself.

{And if one wants a visible break in a RegExp (as literal or string)
ISTM that escaping a real space with a \ should (but doesn't) do it.]
Try .replace(/ +/g, ' ')
or .replace(/[ \t]+/g, ' ')
One might also want to remove residual single leading and trailing
spaces on each line.

<FAQENTRY>

ISTM that FAQ 4.16 is *technically* wrong, in that ASCII should read
UniCode.

More importantly, ISTM that it should mention that \s includes both
horizontal and vertical whitespace; the latter may sometimes be better
untrimmed.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 1 '06 #6
Bosconian wrote:
Richard Cornford wrote:
>Bosconian wrote:
>>I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing
extra spaces from the middle?

Where

"hello world"

becomes

"hello world"

But how are you defining an "extra space"?

Probably you would want to replace and continuous sequence
^^^
any
>of two or more spaces with a single space.

Yes, as you described. The syntax that Evertjan provided
suits my needs exactly.
Those two statements cannot both be true. Evertjan's regular expression
does not do what I described.

Richard.
Aug 2 '06 #7
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ea*******************@news.demon.co.uk...
Bosconian wrote:
>Richard Cornford wrote:
>>Bosconian wrote:
I know that

str.replace(/^\s+|\s+$/g,'');

will trim a string of space, but what about removing
extra spaces from the middle?

Where

"hello world"

becomes

"hello world"

But how are you defining an "extra space"?

Probably you would want to replace and continuous sequence
^^^
any
>>of two or more spaces with a single space.

Yes, as you described. The syntax that Evertjan provided
suits my needs exactly.

Those two statements cannot both be true. Evertjan's regular expression
does not do what I described.

Richard.

Are you talking about

result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

My test produced "hello world" from " hello world ".
Aug 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ragnorack67 | last post: by
5 posts views Thread by Marco Gallo | last post: by
3 posts views Thread by Carlos Santos | last post: by
34 posts views Thread by Registered User | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.