相关疑难解决方法(0)

更换  来自javascript dom文本节点

我正在使用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];
    }) );
  }
})();
Run Code Online (Sandbox Code Playgroud)

有什么建议?

javascript regex html-entities

56
推荐指数
6
解决办法
9万
查看次数

标签 统计

html-entities ×1

javascript ×1

regex ×1