我在SO上发现了一个非常漂亮的小脚本,几乎可以实现我正在寻找的东西。它用维基百科的链接替换每个单词列表的出现。问题是我只希望将第一次出现联系起来。
这是脚本(来自this answer):
function replaceInElement(element, find, replace) {
// iterate over child nodes in reverse, as replacement may increase
// length of child node list.
for (var i= element.childNodes.length; i-->0;) {
var child= element.childNodes[i];
if (child.nodeType==1) { // ELEMENT_NODE
var tag= child.nodeName.toLowerCase();
if (tag!='style' && tag!='script') // special case, don't touch CDATA elements
replaceInElement(child, find, replace);
} else if (child.nodeType==3) { // TEXT_NODE
replaceInText(child, find, replace);
}
}
}
function replaceInText(text, find, replace) {
var match;
var matches= [];
while (match= find.exec(text.data))
matches.push(match);
for (var i= matches.length; i-->0;) {
match= matches[i];
text.splitText(match.index);
text.nextSibling.splitText(match[0].length);
text.parentNode.replaceChild(replace(match), text.nextSibling);
}
}
// keywords to match. This *must* be a 'g'lobal regexp or it'll fail bad
var find= /\b(keyword|whatever)\b/gi;
// replace matched strings with wiki links
replaceInElement(document.body, find, function(match) {
var link= document.createElement('a');
link.href= 'http://en.wikipedia.org/wiki/'+match[0];
link.appendChild(document.createTextNode(match[0]));
return link;
});
我一直试图修改它(没有成功)使用indexOf insed of regex(来自this answer),我认为这会比正则表达式更快:
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
}
所以我的问题是如何在不使用库的情况下将这两者结合起来以获得最有效/最快的结果?
答案 0 :(得分:1)
此处修改了您的代码以执行您想要的操作http://jsfiddle.net/bW7LW/2/
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;
});
});