Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > A robust way to remove white spaces (RegExp)

Reply
Thread Tools

A robust way to remove white spaces (RegExp)

 
 
VK
Guest
Posts: n/a
 
      05-22-2006
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?

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      05-22-2006
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)
 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      05-22-2006
JRS: In article <(E-Mail Removed) .com>
, dated Mon, 22 May 2006 07:23:40 remote, seen in
news:comp.lang.javascript, VK <(E-Mail Removed)> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-25-2006
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"
 
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
Q: most efficient way to remove duplicate spaces in a string? Mark Watson Ruby 13 01-11-2009 01:16 AM
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
more robust way to handle missing modules? dt Perl Misc 1 04-07-2007 10:54 AM
Any programs to trim white space/ remove all white space in HTML file? Ben C HTML 6 01-28-2007 11:41 PM



Advertisments