Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:31:41+00:00 2026-05-27T09:31:41+00:00

I am trying to perform a dictionary look up on some content and my

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:31:42+00:00Added an answer on May 27, 2026 at 9:31 am

    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];
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a dictionary-based NER on some documents. My dictionary, regardless of
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
Trying to perform a single boolean NOT operation, it appears that under MS SQL
I am trying to perform some benchmarks in some graph data structures in C++
I am trying to perform a Calculus Cross Product calculation using Linq and trying
I'm using an ObjectDataSource to perform CRUD operations. For some reason I am getting
I am trying to perform searches in Japanese using the custom google search api
I am trying to perform a method in a background thread using a NSOperationQueue
I'm trying to perform some memory profiling on an applications that is accumulating large
I'm trying to perform a hierarchical clustering using a custom distance measure. I perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.