简体   繁体   English

查找和替换HTML字符串。 忽略网址

[英]Find and Replace HTML Strings. Ignore URLs

I have written a basic script to search the DOM for strings and replace. 我已经编写了一个基本脚本来搜索DOM中的字符串并进行替换。 But in it's current state, it will affect URLs of links too. 但是在当前状态下,它也会影响链接的URL。

For example replacing "example" with "my example" would break the following example; 例如,将“ example”替换为“ my example”将破坏以下示例;

<a href="http://my example.com/my example">My Example</a>

How could I stop this for happening, so the result would in fact return like this? 我怎样才能阻止这种情况的发生,所以结果实际上会像这样返回?

<a href="http://example.com/example">My Example</a>

Is there a way to automatically account for CAPs or would this require a separate search string? 有没有一种方法可以自动计算CAP,还是需要一个单独的搜索字符串?

Here is my code; 这是我的代码;

$(document).ready(function(){

    $("body").children().each(function() {           

        $(this).html($(this).html().replace(/example/g,"my example"));

    })

});

There will be approximately 80 words to find/replace, from a perfromace point of view, is there a better method of handling this? 从性能的角度来看,大约有80个单词可以查找/替换,是否有更好的方法来处理? Short of separate pages per languages of course. 缺少每种语言的单独页面。

If you use .text() instead of .html() it will not change the hrefs. 如果使用.text()而不是.html(),则不会更改hrefs。

$(document).ready(function(){
    $("body").children().each(function() {           
        $(this).text($(this).text().replace(/example/g,"my example"));
    });
});

UPDATE: I should add that this may cause unintended side effects as currently written. 更新:我应该补充一点,这可能会导致当前所写的意外副作用。 I would suggest restricting the .replace operation to atomic elements as follows: 我建议将.replace操作限制为原子元素,如下所示:

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {
        var children = object.children();
        if (children.length > 0) {
            children.each(function() {
                replaceText($(this));
            });
        } else {
            object.text(object.text().replace(/example/g, "my example"));
        };
    };
});

UPDATE: Here is an alternative approach that works for free-floating text nodes. 更新:这是一种适用于自由浮动文本节点的替代方法。

$(document).ready(function() {
    replaceText($("body"));

    function replaceText(object) {

        //first we handle text nodes inside the element itself
        handleTextNodes(object); 

        //then we iterate down into the child elements 
        var children = object.children();
        children.each(function() {
            replaceText($(this));
        });
    };

    function handleTextNodes(object) {
        object.contents().filter(function() {
            return this.nodeType == 3;
        }).each(function() {
            textNodeReplace(this);
        });
    };

    function textNodeReplace(node) {
        var text = node.nodeValue;
        node.nodeValue = text.replace(/example/g, "my example");
    };
});

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

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