如何使用JavaScript或jQuery删除多余的空格? [英] How to remove extra white spaces using javascript or jquery?

查看:131
本文介绍了如何使用JavaScript或jQuery删除多余的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <! -  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;>样式< / div> < div style =clear:both; border-top:1px solid rgb(216,216,216); padding-top:4px;> <! - 产品描述 - > < 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;>精细色调条纹面料< / 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;>肩章和袖子标签< / 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;>胸袋上的金属徽标板< / div> 

当我使用jquery阅读时,我得到 .text() code>包含很多空格和/ n,但没有html标记。

使用jquery或纯javascript?

解决方案

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

它使用正则表达式( /.../ )在整个元素的文本中搜索一个或多个( + )空白字符( \ s ) ( g ,全局修饰符,它查找所有匹配,而不是在第一次匹配后停止),并用一个空格替换()。


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?

解决方案

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 (" ").

这篇关于如何使用JavaScript或jQuery删除多余的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆