Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > remove extra spaces with regexp

Reply
Thread Tools

remove extra spaces with regexp

 
 
Bosconian
Guest
Posts: n/a
 
      08-01-2006
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.


 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      08-01-2006
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)
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      08-01-2006
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.

 
Reply With Quote
 
Bosconian
Guest
Posts: n/a
 
      08-01-2006
"Evertjan." <(E-Mail Removed)> wrote in message
news:Xns9812C83372239eejj99@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.


 
Reply With Quote
 
Bosconian
Guest
Posts: n/a
 
      08-01-2006
"Richard Cornford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-01-2006
JRS: In article <Xns9812C83372239eejj99@194.109.133.242>, dated Tue, 1
Aug 2006 17:40:49 remote, seen in news:comp.lang.javascript, Evertjan.
<(E-Mail Removed)> posted :
>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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      08-02-2006
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.


 
Reply With Quote
 
Bosconian
Guest
Posts: n/a
 
      08-03-2006
"Richard Cornford" <(E-Mail Removed)> wrote in message
news:eapkut$khe$1$(E-Mail Removed)...
> 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 ".


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
A robust way to remove white spaces (RegExp) VK Javascript 3 05-25-2006 03:00 PM



Advertisments