如何使用 javascript 或 jquery 删除多余的空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2898192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove extra white spaces using javascript or jquery?
提问by Amr Elgarhy
I got HTML element contains this:
我得到的 HTML 元素包含这个:
<!--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("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") no-repeat scroll 0pt 4px transparent; padding-left: 12px;">fine tonal striped fabric</div> <div style="background: url("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") no-repeat scroll 0pt 4px transparent; padding-left: 12px;">epaulettes and sleeve tab</div> <div style="background: url("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") 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.
当我使用 jquery 阅读它时,我得到.text()文本之间包含大量空格和 /n 但没有 html 标签。
How to remove all these white spaces and return the clean text using jquery or pure javascript?
如何删除所有这些空格并使用 jquery 或纯 javascript 返回干净的文本?
回答by Matt S
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 (" ").
这使用正则表达式 ( /.../)在整个元素的文本中搜索一个或多个 ( +) 空白字符 ( \s)(g,全局修饰符,它会查找所有匹配项而不是在第一个匹配项之后停止),并将每个空白字符 ( )替换为一个空格 ( " ")。
回答by Adam Grant
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
您可能希望将多余的空格变成单个空格,但完全删除前导和尾随空格。为此,添加另一个.replace
s.replace(/\s+/g, " ").replace(/^\s|\s$/g, "");
For
为了
"this is my string"
Update
更新
s.replace(/\s+/g, " ").trim()
Thanks, @Pier-Luc Gendreau
谢谢,@Pier-Luc Gendreau
回答by Peter Bailey
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
然后清理它并将其应用于您的jQuery
$.trim( $element.text().replace( /[\s\n\r]+/g, ' ' ) )
回答by Thivan Mydeen
This code working fine, Here I have remove extra white spaces in TextEditor.
这段代码工作正常,我在这里删除了 TextEditor 中的多余空格。
var htmlstring = $("#textareaCode").html().replace(/( )*/g, "");
for more Deatils refer: http://www.infinetsoft.com/Post/How-to-remove-nbsp-from-texteditor-in-jQuery/1226#.V0J-2fl97IU
更多细节请参考:http: //www.infinetsoft.com/Post/How-to-remove-nbsp-from-texteditor-in-jQuery/1226#.V0J-2fl97IU

