javascript 删除javascript中的非中断空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19549524/
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
removing non-break-spaces in javascript
提问by user2014429
I am having trouble removing spaces from a string. First I am converting the div to text();
to remove the tags(which works) and then I'm trying to remove the " 
" part of the string, but it wont work. Any Idea what I'm doing wrong. Thanks.
我无法从字符串中删除空格。首先,我将 div 转换text();
为删除标签(有效),然后我试图删除 
字符串的“ ”部分,但它不起作用。任何想法我做错了什么。谢谢。
newStr = $('#myDiv').text();
newStr = newStr.replace(/ /g,'');
$('#myText').val(newStr);
<html>
<div id = "myDiv"><p>remove space</p></div>
<input type = "text" id = "myText" />
</html>
回答by Denys Séguret
When you use the text
function, you're not getting HTML but text : the
entities have been changed to spaces.
当您使用该text
函数时,您得到的不是 HTML 而是文本:
实体已更改为空格。
So simply replace spaces :
所以只需替换空格:
var str = "?a?????b???", // bunch of NBSPs
newStr = str.replace(/\s/g,'');
console.log(newStr)
If you want to replace only the spaces coming from
do the replacement before the conversion to text :
如果您只想替换来自的空格,
请在转换为文本之前进行替换:
newStr = $($('#myDiv').html().replace(/ /g,'')).text();
回答by Fabrício Matté
.text()
/textContent
do not contain HTML entities (such as
), these are returned as literal characters. Here's a regular expression using the non-breaking spaceUnicode escape sequence:
.text()
/textContent
不包含 HTML 实体(例如
),它们作为文字字符返回。这是一个使用不间断空格Unicode 转义序列的正则表达式:
var newStr = $('#myDiv').text().replace(/\u00A0/g, '');
$('#myText').val(newStr);
It is also possible to use a literal non-breaking space character instead of the escape sequence in the Regex, however I find the escape sequence more clear in this case. Nothing that a comment wouldn't solve, though.
也可以在 Regex 中使用文字不间断空格字符代替转义序列,但是我发现在这种情况下转义序列更清晰。不过,没有评论解决不了的问题。
It is also possible to use .html()
/innerHTML
to retrieve the HTML containing HTML entities, as in @Dystroy's answer.
也可以使用.html()
/innerHTML
来检索包含 HTML 实体的 HTML,如@Dystroy 的回答。
Below is my original answer, where I've misinterpreted OP's use case. I'll leave it here in case anyone needs to remove
from DOM elements' text content
以下是我的原始答案,其中我误解了 OP 的用例。我会把它留在这里以防有人需要
从 DOM 元素的文本内容中删除
[...] However, be aware that re-setting the .html()
/innerHTML
of an element means trashing out all of the listeners and data associated with it.
[...] 但是,请注意,重新设置元素的.html()
/innerHTML
意味着删除所有与之关联的侦听器和数据。
So here's a recursive solution that only alters the text content of text nodes, without reparsing HTML nor any side effects.
所以这里有一个递归解决方案,它只改变文本节点的文本内容,而不重新解析 HTML 也没有任何副作用。
function removeNbsp($el) {
$el.contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(/\u00A0/g, '');
} else {
removeNbsp( $(this) );
}
});
}
removeNbsp( $('#myDiv') );