473,406 Members | 2,387 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,406 software developers and data experts.

A robust way to remove white spaces (RegExp)

VK
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
between tag borders in the source code.

1) left border defined by gt sign >
2) right border defined by lt sign <
3) If the content between left and right borders consists only of white
spaces it has to be removed.
4) Content consists of white spaces only if it contains only \n, \r,
\t, \f, space (\u0020) in any amount and any combinations.
Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
character.

Thus say the outcome from:

<foo>
<bar>Foobar</bar>
</foo>

will be:

<foo><bar>Foobar</bar></foo>

Anyone knows of a similar RegExp?

May 22 '06 #1
3 11489
VK wrote on 22 mei 2006 in comp.lang.javascript:
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
between tag borders in the source code.

1) left border defined by gt sign >
2) right border defined by lt sign <
3) If the content between left and right borders consists only of white
spaces it has to be removed.
4) Content consists of white spaces only if it contains only \n, \r,
\t, \f, space (\u0020) in any amount and any combinations.
Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
character.

Thus say the outcome from:

<foo>
<bar>Foobar</bar>
</foo>

will be:

<foo><bar>Foobar</bar></foo>

Anyone knows of a similar RegExp?


myResult = myString.replace(/\s/g,'')
\s is equivalent to [ \f\n\r\t\v]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 22 '06 #2
JRS: In article <11**********************@i39g2000cwa.googlegroups .com>
, dated Mon, 22 May 2006 07:23:40 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> posted :
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
between tag borders in the source code.

1) left border defined by gt sign >
2) right border defined by lt sign <
3) If the content between left and right borders consists only of white
spaces it has to be removed.
4) Content consists of white spaces only if it contains only \n, \r,
\t, \f, space (\u0020) in any amount and any combinations.
Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
character.

Thus say the outcome from:

<foo>
<bar>Foobar</bar>
</foo>

will be:

<foo><bar>Foobar</bar></foo>

Anyone knows of a similar RegExp?


Seems easy

S = "<foo>\n <bar>Foobar</bar>\n</foo>"

alert(S.replace(/>\s+</g, "><"))

Note : \u0020 is not whitespace, but it is a representation of unit
whitespace in common Unicode characters.

--
© 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.
May 22 '06 #3
Dr John Stockton wrote:
Note : \u0020 is not whitespace, but it is a representation of unit
whitespace in common Unicode characters.


It is the Unicode escape sequence representation of _one_ Unicode SPACE
character (U+0020).
PointedEars
--
When you have eliminated all which is impossible, then
whatever remains, however improbable, must be the truth.
-- Sherlock Holmes in Sir Arthur Conan Doyle's
"The Blanched Soldier"
May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
4
by: Purdy | last post by:
I have an asp.net application. i export to excel, in exporting to excel i use an xslt to define the columns and look. on one of the fields i need the word 'qty' but i need it to look like ' ...
3
by: Dante | last post by:
I need a regular expression to remove all spaces from a string. The best I could do was this: var spacefix = / /gi; var out = data.replace(spacefix,""); Thanks so much, Dante.
3
by: Aaron | last post by:
is there a way to remove all white spaces in the html output? ie. everything as one line.
8
by: Jan | last post by:
Hi there, Is there a way to remove spaces from a line of text? For example: vr 12-12-79 city village state name I want it to be:
2
by: Jan | last post by:
Hi, Is there a way to remove the white spaces between text? For example: "Section 1 is oke fine" I want it to be,
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
7
by: Bosconian | last post by:
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"
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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.