简体   繁体   English

Javascript RegEx替换HTML标记内的所有字符

[英]Javascript RegEx replace all characters not within HTML tags

Looking for a bit of help, my regex is a bit rusty... 寻找一点帮助,我的正则表达式有点生锈...

I'm trying to replace all characters not within HTML tags in javascript by a character. 我正在尝试用字符替换不在javascript中HTML标记内的所有字符。

For example replace those characters by a dash "-", 例如,用破折号“-”替换这些字符,

<div class="test">Lorem Ipsum <br/> Dolor Sit Amet</div>

Would be replaced by: 将被替换为:

<div class="test">------------<br/>--------------</div>

So I'm looking for 所以我在找

str.replace(/YourMagicalRegEx/g, '-');

Please help, I get how to return text not within html tags with regex, text within html tags with regex, but all characters not within html tags seems quite tricky...! 请帮忙,我得到了如何用正则表达式返回不在html标签内的文本,如何用正则表达式返回在html标签内的文本,但是不在html标签内的所有字符似乎很棘手...!

Additional Challenge: Must be IE7 and up compatible. 附加挑战:必须兼容IE7及更高版本。

Using jQuery: 使用jQuery:

html = '<div class="test">Lorem Ipsum <br/> Dolor Sit Amet</div>';
node = $("<div>" + html + "</div>");
node.find('*').contents().each(function() {
    if(this.nodeType == 3)
        this.nodeValue = Array(this.nodeValue.length).join('-')

});
console.log(node.html())

(I don't have IE7 at hand, let me know if this works). (我手头没有IE7,请让我知道是否可行)。

If you prefer regular expressions, it goes like this: 如果您更喜欢正则表达式,则如下所示:

html = html.replace(/<[^<>]+>|./g, function($0) {
    return $0[0] == '<' ? $0 : '-';
});

Basically, we replace tags with themselves and out-of-tags characters with dashes. 基本上,我们将标签替换为自己,将标签外的字符替换为破折号。

Instead of using a regex-only approach, you can find all text nodes within the document and replace their content with hyphens. 您可以在文档中找到所有文本节点,然后将其内容替换为连字符,而不是仅使用正则表达式的方法。

Using the TreeWalker API: 使用TreeWalker API:

 var tree = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);

 while (tree.nextNode()) {
     var textNode = tree.currentNode;
     textNode.nodeValue = textNode.nodeValue.replace(/./g, '-');
 }

A recursive solution: 递归解决方案:

function findTextNodes(node, fn){
  for (node = node.firstChild; node;node=node.nextSibling){
    if (node.nodeType === Node.TEXT_NODE) fn(node);
    else if(node.nodeType === Node.ELEMENT_NODE && node.nodeName !== 'SCRIPT') findTextNodes(node, fn);
  }
}


findTextNodes(document.body, function (node) {
  node.nodeValue = node.nodeValue.replace(/./g, '-');
});

The predicate node.nodeName !== 'SCRIPT' is required to prevent the function from replacing any script content within the body. 谓词node.nodeName !== 'SCRIPT'是必需的,以防止函数替换体内的任何脚本内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM