Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove extra white spaces using javascript or jquery?

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&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&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&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?

like image 857
Amr Elgarhy Avatar asked May 24 '10 15:05

Amr Elgarhy


People also ask

How do I get rid of extra space between words in JavaScript?

"; string = string. replace(/\s+/g, " ");

How do I remove extra space between words in jQuery?

The $. trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

How do I get rid of extra white spaces in a string?

If you are just dealing with excess whitespace on the beginning or end of the string you can use trim() , ltrim() or rtrim() to remove it. If you are dealing with extra spaces within a string consider a preg_replace of multiple whitespaces " "* with a single whitespace " " .

How do I remove spaces from a node js string?

You can remove the whitespace in a string by using a string. replace(/\s/g, "") regular expression or a string. split(" "). join("") method.


2 Answers

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

This uses a regular expression (/.../) to search for one or more (+) whitespace characters (\s) throughout the element's text (g, the global modifier, which finds all matches rather than stopping after the first match) and replace each with one space (" ").

like image 118
Matt S Avatar answered Oct 16 '22 20:10

Matt S


For the string

"    this  is my   string       " 

You probably want to make the excessive spaces single spaces, but remove the leading and trailing spaces entirely. For that, add another .replace

s.replace(/\s+/g, " ").replace(/^\s|\s$/g, ""); 

For

"this is my string" 

Update

s.replace(/\s+/g, " ").trim() 

Thanks, @Pier-Luc Gendreau

like image 35
Adam Grant Avatar answered Oct 16 '22 20:10

Adam Grant