remove extra spaces with regexp

B

Bosconian

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.
 
E

Evertjan.

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,' ');
 
R

Richard Cornford

Bosconian said:
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.
 
B

Bosconian

Evertjan. said:
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, that's it--thanks! I just learned something new.
 
B

Bosconian

Richard Cornford said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 1
Aug 2006 17:40:49 remote, seen in Evertjan.
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.
 
R

Richard Cornford

Bosconian said:
^^^
any

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.
 
B

Bosconian

Richard Cornford said:
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 ".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top