在字典Javascript中找到多个关键词 [英] find multiple key words within a dictionary Javascript

查看:99
本文介绍了在字典Javascript中找到多个关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行一个字典查看一些内容,我的方法到目前为止,我只能查找单个单词,因为我正在使用拆分('')方法在空间上分裂。我刚刚碰到一个堵塞,并且想知道你们有没有坚实的输入。就像我在我的字典中有两个字的钥匙怎么办?像下面的

  var dictionary = {Darth Vader:坏屁股父亲,卢克:坏的儿子屁股,
天行者:一些绝地骑士的姓氏,卢克天空之王:坏屁股的儿子}

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

  var body ='< div class =activity-inner >这是一个测试词汇表部分,用于测试星球大战词汇表的亮点。像Luke Skywalker和Darth Vader,Luke和Skywalker。 < / DIV> < div class =activity-inner> Hello glossary again Luke Skywalker Darth Vader< / div>'; 
document.write(body);
var matches = getElementsByClassName(activity-inner); (int i = 0; i< matches.length; i ++;){
var content = matches [i] .innerHTML;

document.write(content);
var words = content.split(''); (var j = 0; j< words.length; j ++){



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



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

上面的例子不行。然而,我的字典中的一些事情会是这样的。我应该怎么做,如果可能的话也可以避免这种情况?谢谢!

解决方案

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



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

  //示例
var dictionary = {Darth Vader:坏屁股父亲,卢克:坏屁股的儿子,
天行者:绝地骑士之姓,路加福音,坏屁股之子}
content =....;

var pattern = [],
key;
(键入字典){
//消除密钥,并将其推送到列表中
pattern.push(key.replace(/([[^ $。 (){}])/ g,'\\ $ 1'));
}
pattern =(?:+ pattern.join()|(?:)+); // Create pattern
pattern = new RegExp(pattern,g);

//遍历字符串,并替换每一次匹配的令牌
content = content.replace(pattern,function(full_match){
return dictionary [full_match];
});


I am trying to perform a dictionary look up on some content and my approach so far I can only look up single words because I am using the split(' ') method splitting on just the space. I just ran into a sorta blockage and was wondering if guys you had any solid input. like what if I have keys in my dictionary that are of two words. Like below

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"} 

this is the code I got so far and it works for single words obviously but not muliplte key words. getElementsByClassName is a function to return an array of all classes found.

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);
}

The above example wouldn't work. However Some of the things in my dictionary are gonna be like this. How should I go about this and maybe avoid case as well if at all possible? Thanks!

解决方案

Construct a RegEx, consisting of all keys of the dictionary (to prevent a replacement from being replaced again). Then, use String.replace(pattern, replace_function) as shown below.

Demo: 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];
});

这篇关于在字典Javascript中找到多个关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆