473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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 16435
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ragnorack67 | last post by:
Hi, I need some advice on what to do. I have this MySQL insert file, with about 30,000 records. One of the datafields has names in it. When this list was put together, apparently there was extra...
5
by: Marco Gallo | last post by:
I've been trying to get rid of extra spaces in a table that I created with addresses in it. For instance in a field called TEST, I got the following address: " 1 main st Apt A "...
3
by: Carlos Santos | last post by:
I tried the example from the RijndaelManaged class in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrijndaelmanagedclasstopic.asp and I...
0
by: ChrisM | last post by:
Hi, Got a strange one here. I have a report which is part of my dotnet application. It prints fine except for one thing: It adds extra spaces to certain things. eg, required text: 'Service...
3
by: VK | last post by:
If it was already answered somewhere, I'll be glad to be pointed to (after the necessary comments on my search abilities :-) I need as booletproof as possible way to strip out whitespaces from...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
1
by: buggtb | last post by:
Hi Guys, I've been given the joyous task of updating some very old scripts at work and I could do with a little help. Our Unix system dumps text files to pseudo spoolers that our windows...
2
code green
by: code green | last post by:
I am trying to write a simple function that will take a string containing an address line or business name and return it nicely formatted. By this I mean extra spaces removed and words capitalised....
2
by: SwapnilD | last post by:
I'm implementing a feature which reads comma separated txt file from server(one line at a time). Format of file is fixed, There are 3 columns on each row. After reading the row from file I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.