I want to add a condition if I have words that inside "" DOUBLE QUOTES var wordsToHighlight =' "word1, word2" ' means highlight word1 word2 in the whole text
exp var wordsToHighlight = ' "a reference, server" ' mean highlight a reference server in the whole text
my problem here needs to highlight the text that inside double quotes in the whole text
Explication:
The
*its a truncation and works wellthe
?to highlight words+ n characters.split(/"([^"]+)"|\s+/).filter(Boolean)It will split the string with double quotes substrings while pushing the substring between double quotes into the resulting array (String#split always pushes the captured substrings into the resulting array), and with 1+ whitespaces and .filter(Boolean) will remove empty items that may result during the split operation.
var row = {
"Abstract": "I have a reference server for reference and just a server here server test."
};
var wordsToHighlight = ' "a reference, server" jus? fo* ';
var result = row["Abstract"];
wordsToHighlight.split(/"([^"]+)"|\s+/).filter(Boolean).forEach(function (word) {
word = word.replace(/\*/g, '\\S*').replace(/\?/g, '.').replace(/\"/g, '.');
result = result.replace(new RegExp('(\\s|^)(' + word + ')(?=\\s|$)', "gi"),'$1<span style="background-color:yellow;">$2</span>');
});
document.querySelector("#result").innerHTML = result;
<div id="result"></div>
the result that i Expected:
