获取Unicode字符串的解码版本?

时间:2015-09-21 09:32:16

标签: javascript html xslt unicode

我将一个变量写入我的HTML元素中,如下所示:

document.getElementById('myDiv').innerHTML = '<xsl:value-of select="@value"/>'; 

然而它的写法如下:

TECHNO A.&#x15E;.

应该是:

TECHNO A.Ş.

如何使用Javascript手动获取该Unicode字符串的解码版本?

PS:我已经意识到Chrome出现此问题,但Internet Explorer却没有。

1 个答案:

答案 0 :(得分:0)

&amp;#和;是十六进制字符的html解析器转义。所以你需要做的是剥掉他们并通过剩下的15E&#39;这个值(例如):

<!DOCTYPE html>
<div id="myId">
yo oyyoyoyooy &amp;amp;amp;#xAA;suuupspuspupsu
  ssdosudoisduoisudoiud
  &amp;amp;#xFE;
</div>


<script>
document.addEventListener("DOMContentLoaded", function(){
  var nodeIterator = document.createNodeIterator(
  // Node to use as root
    document.getElementById('myId'),

    // Only consider nodes that are text nodes (nodeType 3)
    NodeFilter.SHOW_TEXT,

    // Object containing the function to use for the acceptNode method
    // of the NodeFilter
      { acceptNode: function(node) {
        // Logic to determine whether to accept, reject or skip node
        // In this case, only accept nodes that have content
        // other than whitespace
        if ( /&.*?#x[^;]+;/ig.test(node.data) ) {
          return NodeFilter.FILTER_ACCEPT;
        }
      }
    },
    false
  );

  // Show the content of every non-empty text node that is a child of root
  var node;

  while ((node = nodeIterator.nextNode())) {
    node.nodeValue = node.nodeValue.replace(/&.*?#x([^;]+);/ig, function(match, p1) {
      return String.fromCharCode(parseInt(p1, 16))
    });
  }
});
</script>