简体   繁体   English

HTML标签内的高级JavaScript RegExp替换

[英]Advanced JavaScript RegExp Replacement Within HTML tags

I have javascript code that works pretty well like: 我有运行良好的javascript代码,例如:

var rgx = /MyName/g;
var curInnerHTML = document.body.innerHTML;
curInnerHTML = curInnerHTML.replace(rgx, "<span><span class='myName'>MyNameReplace</span></span>");

The problem is that its matching the regex even in scenarios where it is contained within HTML attributes and what-not. 问题在于,即使在HTML属性中包含正则表达式的情况下,它也匹配正则表达式。 How can I modify the regex so that it will only find it within the content of the HTML? 如何修改正则表达式,使其只能在HTML内容中找到它? For example, in this string 例如,在此字符串中

    <div class="someclass" title="MyName">
MyName
</div>

it currently results like (note the change in the title attribute): 当前结果如下(请注意title属性的更改):

        <div class="someclass" title="<span><span class='myName'>MyNameReplace</span</span>">
<span><span class='myName'>
    MyNameReplace</span></span>
    </div>

But I need it to be (leave the title attribute untouched): 但是我需要它(保持title属性不变):

    <div class="someclass" title="MyName">
<span><span class='myName'>MyNameReplace</span></span>
</div>

Your best bet, and it's a lot easier than it sounds, is not to try to use regex to parse HTML, but to take advantage of the fact that the DOM already has and recursively process the text nodes. 最好的选择(比听起来容易得多) 不是尝试使用正则表达式来解析HTML,而是要利用DOM已经具有并递归处理文本节点这一事实。

Here's an off-the-cuff: Live Example | 这是即时可用的: 实时示例 | Source 资源

// We use this div's `innerHTML` to parse the markup of each replacment
var div = document.createElement('div');

// This is the recursive-descent function that processes all text nodes
// within the element you give it and its descendants
function doReplacement(node, rex, text) {
    var child, sibling, frag;

    // What kind of node did we get?
    switch (node.nodeType) {
        case 1: // Element
            // Probably best to leave `script` elements alone.
            // You'll probably find you want to add to this list
            // (`object`, `applet`, `style`, ...)
            if (node.nodeName.toUpperCase() !== "SCRIPT") {
                // It's an element we want to process, start with its
                // *last* child and work forward, since part of what
                // we're doing inserts into the DOM.
                for (child = node.lastChild; child; child = sibling) {
                    // Before we change this node, grab a reference to the
                    // one that follows it
                    sibling = child.previousSibling;

                    // Recurse
                    doReplacement(child, rex, text);
                }
            }
            break;
        case 3: // Text
            // A text node -- let's do our replacements!
            // The first two deal with the fact that the text node
            // may have less-than symbols or ampersands in it.
            // The third, of course, does your replacement.
            div.innerHTML = node.nodeValue
                                .replace(/&/g, "&amp;")
                                .replace(/</g, "&lt;")
                                .replace(rex, text);

            // Now, the `div` has real live DOM elements for the replacement.
            // Insert them in front of this text node...
            insertChildrenBefore(div, node);
            // ...and remove the text node.
            node.parentNode.removeChild(node);
            break;
    }
}

// This function just inserts all of the children of the given container
// in front of the given reference node.
function insertChildrenBefore(container, refNode) {
    var parent, child, sibling;

    parent = refNode.parentNode;
    for (child = container.firstChild; child; child = sibling) {
        sibling = child.nextSibling;
        parent.insertBefore(child, refNode);
    }
}

Which you'd call like this: 您会这​​样称呼:

doReplacement(document.body,
              /MyName/g,
              "<span><span class='myName'>MyNameReplace</span></span>");

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

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