Vincent
Vincent

Reputation: 1474

Detecting substrings of one or more word in a paragraph

I am making a single-page JavaScript application that will display definitions of terms on the fly. Terms mentioned in that definition will be linked to other definition articles, kind of like Wikipedia.

I wonder if there is a clear-cut way of looking for substrings of one word or more than one word to turn them into links without making unreasonable sacrifices. Here is an example of what I am trying to do.

var paragraph = "William James 'Bill' Murray (born September 21, 1950) is an American actor and comedian. He first gained exposure on Saturday Night Live for which he earned an Emmy Award and later went on to star in comedy films, including Meatballs (1979), Caddyshack (1980), Ghostbusters (1984), What About Bob? (1991), and Groundhog Day (1993). Murray garnered additional critical acclaim later in his career, starring in Lost in Translation (2003), which earned him an Academy Award nomination for Best Actor, the indie comedy-drama Broken Flowers (2005) and a series of films directed by Wes Anderson, including Rushmore (1998), The Royal Tenenbaums (2001), The Life Aquatic with Steve Zissou (2004), The Darjeeling Limited (2007), Fantastic Mr. Fox (2009), Moonrise Kingdom (2012), and The Grand Budapest Hotel (2014)."

var terms = ["Ghostbusters", "Fox", "Moonrise Kingdom", "Meatball"];

var parray = paragraph.split(" ");


for (var i = 0; i < parray.length; i++) {
    if (terms.indexOf(parray[i]) != -1) {
        parray[i] = "<a href=\"?term=\"" + parray[i] + "\"</a>"
    }
}
var newParagraph = parray.join(" ");

console.log(newParagraph);

This example will not catch "Meatball" because it has an 's' at the end and it won't pick up "Moonrise Kingdom" because it will check whether "Moonrise" and "Kingdom" are in the terms array. Any ideas for making this work better?

Upvotes: 2

Views: 63

Answers (2)

Alex K.
Alex K.

Reputation: 175766

Simply replace the terms?

for (var i = 0; i < terms.length; i++) {
    paragraph = paragraph.replace(new RegExp("("+ terms[i] + ")", "ig"), "<a href='?term=$1'>$1</a>");
}

Upvotes: 2

rageandqq
rageandqq

Reputation: 2281

Instead of comparing each split element to the terms array, try the other way around. Compare each element of the array to the original paragraph and see whether it exists within it.

for (var i = 0; i < terms.length; i++) {
    if (paragraph.indexOf(terms[i]) != -1) {
        //You've found a match! Do something here.
    }
}

Upvotes: 1

Related Questions