views:

183

answers:

2

I got HTML element contains this:

  <!--Product Style-->  <div style="float: right; padding-top: 4px; padding-bottom: 5px;">  P6C245RO </div>  <div style="text-transform: uppercase; font-weight: bold; padding-top: 4px; padding-bottom: 5px;">  Style </div>  <div style="clear: both; border-top: 1px solid rgb(216, 216, 216); padding-top: 4px;">  <!--Product Description-->  <div style="font-size: 11px ! important;"></div>  <div style="background: url(&quot;http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif&amp;quot;) no-repeat scroll 0pt 4px transparent; padding-left: 12px;">fine tonal striped fabric</div>  <div style="background: url(&quot;http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif&amp;quot;) no-repeat scroll 0pt 4px transparent; padding-left: 12px;">epaulettes and sleeve tab</div>  <div style="background: url(&quot;http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif&amp;quot;) no-repeat scroll 0pt 4px transparent; padding-left: 12px;">metal logo plate on the chest pocket</div>  

When i read it using jquery i get the .text() contains a lot of spaces and /n in between the text but without the html tags.

How to remove all these white spaces and return the clean text using jquery or pure javascript?

+4  A: 
element.text().replace(/\s+/g, " ");

(Edit: Added global modifier "g")

Matt S
+1 I misread the question. That will work great for spaces and newlines (what the OP said he needed). Be aware that some browsers don't quite implement `\s` correctly once you get into non-ASCII whitespace (http://thinkweb2.com/projects/prototype/whitespace-deviations/), but that's a niggle. :-)
T.J. Crowder
You're missing your `g` (global indicator).
patrick dw
@patrick Yes, after adding the 'g' (global indicator), things worked perfect.
Amr ElGarhy
+1  A: 

You can remove all instances of congruent whitespace and newlines like so

// Note: console.log() requires Firebug

var str = '    this  is \n    some text \r don\'t \t you    know?   ';
console.log( str );

str = str.replace( /[\s\n\r]+/g, ' ' );
console.log( str );

Then to clean it up and apply it to your jQuery

$.trim( $element.text().replace( /[\s\n\r]+/g, ' ' ) )
Peter Bailey