79

我正在使用 javascript 处理 xhtml。我通过连接 nodeType == Node.TEXT_NODE 的所有子节点的 nodeValue 来获取 div 节点的文本内容。

结果字符串有时包含一个不间断的空格实体。如何用常规空格字符替换它?

我的 div 看起来像这样...

<div><b>Expires On</b> Sep 30, 2009 06:30&nbsp;AM</div>

在网上找到的以下建议无效:

var cleanText = text.replace(/^\xa0*([^\xa0]*)\xa0*$/g,"");


var cleanText = replaceHtmlEntities(text);

var replaceHtmlEntites = (function() {
  var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
  var translate = {
    "nbsp": " ",
    "amp" : "&",
    "quot": "\"",
    "lt"  : "<",
    "gt"  : ">"
  };
  return function(s) {
    return ( s.replace(translate_re, function(match, entity) {
      return translate[entity];
    }) );
  }
})();

有什么建议么?

4

9 回答 9

157

这比你做的要容易得多。文本节点中不会包含文字字符串"&nbsp;",它将具有代码 160 的相应字符。

function replaceNbsps(str) {
  var re = new RegExp(String.fromCharCode(160), "g");
  return str.replace(re, " ");
}

textNode.nodeValue = replaceNbsps(textNode.nodeValue);

更新

更容易:

textNode.nodeValue = textNode.nodeValue.replace(/\u00a0/g, " ");
于 2009-09-30T08:51:48.197 回答
37

如果您只需要替换,&nbsp;那么您可以使用更简单的正则表达式:

var textWithNBSpaceReplaced = originalText.replace(/&nbsp;/g, ' ');

此外,您的 div 示例中有一个错字,它说&nnbsp;而不是&nbsp;.

于 2009-09-30T02:25:01.857 回答
12

第一行很混乱。它只需要:

var cleanText = text.replace(/\xA0/g,' ');

这应该就是你所需要的。

于 2009-09-30T14:44:51.327 回答
7

认为当您使用“ var foo = function() {...};”定义函数时,该函数仅在该行之后定义。换句话说,试试这个:

var replaceHtmlEntites = (function() {
  var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
  var translate = {
    "nbsp": " ",
    "amp" : "&",
    "quot": "\"",
    "lt"  : "<",
    "gt"  : ">"
  };
  return function(s) {
    return ( s.replace(translate_re, function(match, entity) {
      return translate[entity];
    }) );
  }
})();

var cleanText = text.replace(/^\xa0*([^\xa0]*)\xa0*$/g,"");
cleanText = replaceHtmlEntities(text);

编辑:另外,仅var在您第一次声明变量时使用“”(您在cleanText变量上使用了两次)。

编辑2:问题是函数名的拼写。你有“var replaceHtml Entites =”。它应该是“var replaceHtml Entit es =

于 2009-09-30T02:24:31.170 回答
6

我用了这个,它奏效了:

var cleanText = text.replace(/&amp;nbsp;/g,"");
于 2010-08-05T09:26:06.267 回答
5
var text = "&quot;&nbsp;&amp;&lt;&gt;";
text = text.replaceHtmlEntites();

String.prototype.replaceHtmlEntites = function() {
var s = this;
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
var translate = {"nbsp": " ","amp" : "&","quot": "\"","lt"  : "<","gt"  : ">"};
return ( s.replace(translate_re, function(match, entity) {
  return translate[entity];
}) );
};

试试这个.....这对我有用

于 2012-11-24T08:04:15.097 回答
1

删除所有此类符号之间的所有&内容。;如果你只是想摆脱它们。

text.replace(/&.*;/g,'');
于 2015-03-24T11:19:53.003 回答
0

对我来说替换不起作用......试试这个代码:

str = str.split("&quot;").join('"');
于 2018-02-25T07:01:07.913 回答
0

破解此问题的一种方法是将任何空行替换为两个或多个空格,并带有一些换行符和一个标记。然后发布降价,仅用该标记替换段落以换行。

// replace empty lines with "EMPTY_LINE"
rawMdText = rawMdText.replace(/\n  +(?=\n)/g, "\n\nEMPTY_LINE\n");
// put <br> at the end of any other line with two spaces
rawMdText = rawMdText.replace(/  +\n/, "<br>\n");

// parse
let rawHtml = markdownParse(rawMdText);

// for any paragraphs that end with a newline (injected above) 
// and are followed by multiple empty lines leading to
// another paragraph, condense them into one paragraph
mdHtml = mdHtml.replace(/(<br>\s*<\/p>\s*)(<p>EMPTY_LINE<\/p>\s*)+(<p>)/g, (match) => {
  return match.match(/EMPTY_LINE/g).map(() => "<br>").join("");
});

// for basic newlines, just replace them
mdHtml = mdHtml.replace(/<p>EMPTY_LINE<\/p>/g, "<br>");

这样做的目的是找到每个新行,只有几个空格+。它使用前瞻,以便它从正确的位置开始进行下一次替换,如果没有它,它将连续两行中断。

然后 Markdown 会将这些行解析为只包含标记“EMPTY_LINE”的段落。因此,您可以浏览 rawHtml 并用换行符替换它们。

作为奖励,如果存在,替换功能会将所有换行段落压缩为上段和下段。

实际上,您可以像这样使用它:

A line with spaces at end  
  
  
and empty lines with spaces in between will condense into a multi-line paragraph.

A line with no spaces at end
  
  
and lines with spaces in between will be two paragraphs with extra lines between.

输出将是这样的:

<p>
  A line with spaces at end<br>
  <br>
  <br>
  and empty lines with spaces in between will condense into a multi-line paragraph.
</p>

<p>A line with no spaces at end</p>
<br>
<br>
<p>and lines with spaces in between will be two paragraphs with extra lines between.</p>
于 2021-03-10T21:07:16.353 回答