TL;DR
How do I return strings that contain a substring, but only if is not surrounded by letters?
CONTEXT
I'm creating a translation tool to a fictional language. I have a lexicon stored in a JSON object and am using the following code to translate each word from the input to another word for the output:
//loop through lexicon and if inputted word is in values, return corresponding key -> NEED A WAY TO GET
for(var word in inputArray){
for(var key in lexicon){
var value = lexicon[key];
var term = inputArray[word];
// check if the term appears as a value in the selected lexicon
if(value.indexOf(term) !== -1){
// if key contains commas, then take only what's before the first comma
if(key.indexOf(',') !== -1){
match = key.substring(0, key.indexOf(','));
// if there are no commas, return the whole key
} else{
match = key;
}
outputArray.push(match)
break;
}
}
}
PROBLEM
Although this method works for longer words, shorter words such as "hi" will be matched to the first value found in the lexicon, which could be another word, such as "thing". Since some of the object values contain comma-separated words, I need a way to pull only words which are NOT surrounded by letters.
DESIRED RESULT
The translation of "thing" will not be returned if I type "hi", but the translation of "hi,hello,goodday" or "hello,hi" will be returned.