【问题标题】:find multiple key words within a dictionary Javascript在字典 Javascript 中查找多个关键词
【发布时间】:2011-12-07 10:14:58
【问题描述】:

我正在尝试对某些内容进行字典查找,而我的方法到目前为止我只能查找单个单词,因为我使用 split(' ') 方法仅在空间上进行拆分。我刚刚遇到了一个障碍,想​​知道你们是否有任何可靠的意见。就像我的字典中有两个单词的键一样。如下所示

var  dictionary = { "Darth Vader" : "Bad Ass Father", "Luke": "Son of the Bad Ass",
"Skywalker" : "Last name of some Jedi Knights", "Luke SkyWalker" : "Son of the Bad Ass"} 

这是我到目前为止得到的代码,它显然适用于单个单词,但不适用于多个关键词。 getElementsByClassName 是一个函数,用于返回找到的所有类的数组。

var body = '<div class="activity-inner"> This is a test glossary body paragraph to test glossary highlight of the Star Wars vocabulary. like Luke Skywalker and Darth Vader, Luke and Skywalker. </div> <div class="activity-inner">Hello glossary again here Luke Skywalker Darth Vader</div>';
document.write( body);
var matches = getElementsByClassName("activity-inner");
for (int i = 0; i < matches.length; i++;) {
var content = matches[i].innerHTML;
document.write(content);
var  words = content.split(' ');



for (var j = 0; j < words.length; j++) {
var temp = words[j].replace(/[^\w\s]|_/g, "")
     .replace(/\s+/g, " ").toLowerCase();
 if (temp in dictionary) {
words[j] = "<span class='highlight' style='color:green;'>"+words[j]+"</span>";
}
document.write(words[j] +"<br />");



}
body = words.join(' ');
document.write( "<br /> <br />" + body);
}

上面的例子行不通。然而,我的字典里有些东西会是这样的。我应该怎么做,如果可能的话,我应该如何避免这种情况?谢谢!

【问题讨论】:

  • for (var match in matches) 不应用于遍历数组。除此之外,jQuery 可能会让你的工作更轻松。
  • 哦,好的,谢谢。将其更改为顺序计数器循环。
  • 在第二个循环中word 是索引,而不是索引元素。被索引的元素是words[word](你可以在下一行得到它)。
  • 感谢 katspaugh,我将所有 for 循环更改为使用顺序计数器循环,但我没有更新此代码。但你是对的,不过谢谢!
  • 不要使用document.write。请改用appendChildinnerHTML

标签: javascript search dictionary


【解决方案1】:

构造一个正则表达式,由字典的所有键组成(以防止再次替换替换)。然后,使用String.replace(pattern, replace_function),如下所示。

演示:http://jsfiddle.net/Z7DqF/

// Example
var dictionary = { "Darth Vader" : "Bad Ass Father", "Luke": "Son of the Bad Ass",
"Skywalker" : "Last name of some Jedi Knights", "Luke SkyWalker" : "Son of the Bad Ass"}
    content = "....";

var pattern = [],
    key;
for (key in dictionary) {
    // Sanitize the key, and push it in the list
    pattern.push(key.replace(/([[^$.|?*+(){}])/g, '\\$1'));
}
pattern = "(?:" + pattern.join(")|(?:") + ")"; //Create pattern
pattern = new RegExp(pattern, "g");

// Walk through the string, and replace every occurrence of the matched tokens
content = content.replace(pattern, function(full_match){
    return dictionary[full_match];
});

【讨论】:

  • 嘿,谢谢!我只有一个问题。我如何将这个词保留在那里,然后只附加定义?就像坏蛋的卢克之子。
  • return dictionary[full_match]; 替换为return full_match + dictionary[full_match];。如果您想要一个空格,请将+ 替换为+ " " +
  • 标记为答案。谢谢!该演示链接也非常有帮助。
  • 嘿 ROb W。我非常感谢您的帮助。不过,关于您的实施,我有两个问题。第一个是:我相信这匹配不完整的单词。就像我的字典中的 short 一样,它与该词很快匹配。我将如何阻止这一切?第二个不那么重要但很好的是:我将如何制作它以便每个内容只匹配一次?所以 short 不会被定义两次。希望这是有道理的
  • @Jared 我的答案已经准备好,您必须现在发布您的问题。另外,你能在这条链上删除一些不相关的 cmets 吗?这会让其他人更容易阅读;)顺便说一句,为了不匹配,只需在括号周围添加 \\b :pattern = "\\b(?:" + pattern.join(")\\b|\\b(?:") + ")\\b"; //Create pattern
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多