Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用链接替换列表中每个单词的第一次出现_Javascript_Regex_Dom - Fatal编程技术网

Javascript 用链接替换列表中每个单词的第一次出现

Javascript 用链接替换列表中每个单词的第一次出现,javascript,regex,dom,Javascript,Regex,Dom,我已经找到了一个非常好的小脚本上,所以这几乎是我所寻找的。它用指向维基百科的链接取代了单词列表中出现的每一个单词。问题是我只想把第一次发生的事情联系起来 以下是脚本(来自): 我一直在尝试修改它(没有成功)以使用indexOf代替regex(从),我认为这将比regex更快: var words = ["keyword","whatever"]; var text = "Whatever, keywords are like so, whatever... Unrelated, I now kn

我已经找到了一个非常好的小脚本上,所以这几乎是我所寻找的。它用指向维基百科的链接取代了单词列表中出现的每一个单词。问题是我只想把第一次发生的事情联系起来

以下是脚本(来自):

我一直在尝试修改它(没有成功)以使用indexOf代替regex(从),我认为这将比regex更快:

var words = ["keyword","whatever"];
var text = "Whatever, keywords are like so, whatever... Unrelated, I now know " +
           "what it's like to be a tweenage girl. Go Edward.";
var matches = []; // An empty array to store results in.

//Text converted to lower case to allow case insensitive searchable.
var lowerCaseText = text.toLowerCase();
for (var i=0;i<words.length;i++) { //Loop through the `words` array
    //indexOf returns -1 if no match is found
    if (lowerCaseText.indexOf(words[i]) != -1) 
        matches.push(words[i]);    //Add to the `matches` array
}
var words=[“关键字”,“任意”];
var text=“不管怎样,关键字都是这样,不管怎样……不相关,我现在知道了”+
“做一个十来岁的女孩是什么感觉。去吧,爱德华。”;
变量匹配=[];//用于存储结果的空数组。
//文本转换为小写,允许不区分大小写的搜索。
var lowerCaseText=text.toLowerCase();

对于(var i=0;i,这里是您的代码修改,以执行您想要的操作

函数replaceInit(元素、查找、替换){
找到的变量={},
replaceInElement=函数(元素、查找、替换、初始化){
变量子,标记,
len=element.childNodes.length,
i=0,
replaceInText=函数(文本、查找、替换){
var len=find.length,
指数,i=0;
对于(;i
我看到所有的“Lorem”都被链接,而不是每个元素的第一个,这就是代码的工作原理,但它只替换了第一个副本。它可以被修改,只需要跟踪找到了哪些表达式。在这里,我还必须反转元素的循环,否则它会从下到上而不是从上到下。
var words = ["keyword","whatever"];
var text = "Whatever, keywords are like so, whatever... Unrelated, I now know " +
           "what it's like to be a tweenage girl. Go Edward.";
var matches = []; // An empty array to store results in.

//Text converted to lower case to allow case insensitive searchable.
var lowerCaseText = text.toLowerCase();
for (var i=0;i<words.length;i++) { //Loop through the `words` array
    //indexOf returns -1 if no match is found
    if (lowerCaseText.indexOf(words[i]) != -1) 
        matches.push(words[i]);    //Add to the `matches` array
}
function replaceInit(element, find, replace) {

    var found = {},
        replaceInElement = function(element, find, replace, init) {

            var child, tag, 
                len = element.childNodes.length, 
                i = 0,
                replaceInText = function(text, find, replace) {

                    var len = find.length,
                        index, i = 0;

                    for (; i < len; i++) {

                        index = text.data.indexOf(find[i]);

                        if (index !== -1 && found && !found[find[i]]) {

                            found[find[i]] = true;
                            text.splitText(index);
                            text.nextSibling.splitText(find[i]);
                            text.parentNode.replaceChild(replace(find[i]), text.nextSibling);
                            return;
                        };
                    };
                };

            // iterate over child nodes in reverse, as replacement may increase length of child node list.
            for (; i < len; i++) {

                child = element.childNodes[i];

                if (child.nodeType == 1) { // ELEMENT_NODE
                    tag = child.nodeName.toLowerCase();

                    if (tag != 'style' && tag != 'script') {
                        replaceInElement(child, find, replace);
                    }

                } else if (child.nodeType == 3) { // TEXT_NODE
                    replaceInText(child, find, replace);
                }
            }
        };
    replaceInElement(element, find, replace);
};

// keywords to match. This *must* be a 'g'lobal regexp or it'll fail bad
var find = 'Lorem Ipsum bla'.split(' ');

$(function() {

    // replace matched strings with wiki links
    replaceInit(document.body, find, function(str) {
        var link = document.createElement('a');
        link.href = 'http://en.wikipedia.org/wiki/' + str;
        link.appendChild(document.createTextNode(str));
        return link;
    });
});​