2

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 well

  • the ? 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:

enter image description here

MokiNex
  • 857
  • 1
  • 8
  • 21
  • It seems you could write `"a reference" server jus? fo*` to make it work with the current code. Do you have any control over the contents of the `wordsToHighlight`? Or is it user-defined? – Wiktor Stribiżew Apr 10 '18 at 12:04

1 Answers1

2

You need to parse the words to highlight in a bit more sophisticated way: split out double quoted strings and chunks of non-whitespace. The latter ones can be added to the resulting array as is, but the contents from the double quotes should be split with a comma (and any enclosing spaces).

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"];
var wordsTH2=[], m;
var rx = /"([^"]+)"|\S+/g;
while (m=rx.exec(wordsToHighlight)) {
  if (m[1]) {
     var arr = m[1].split(/\s*,\s*/);
     for (var i=0; i<arr.length;i++) {
        wordsTH2.push(arr[i]);
     }
  } else {
    wordsTH2.push(m[0]);
  }
}
wordsTH2.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>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You may use ideas on how to push an array to an array [from here](https://stackoverflow.com/questions/4156101/javascript-push-array-values-into-another-array). – Wiktor Stribiżew Apr 10 '18 at 12:12
  • thanks, my javascript birt doesn't recognize this sentence `var r of m[1].split(/\s*,\s*/)` missing ; after for-loop initializer? – MokiNex Apr 10 '18 at 12:44
  • Sure, it must be `for (var r of m[1].split(/\s*,\s*/))`. But there are other ways to add the matches obtained via `m[1].split(/\s*,\s*/)` – Wiktor Stribiżew Apr 10 '18 at 12:50
  • could you change it in your solution BIRT script didn't know `for (var r of m[1].split(/\s*,\s*/))` he dont have the feature `of` in his library – MokiNex Apr 10 '18 at 12:59
  • 1
    Use the easiest `for (var i=0; i – Wiktor Stribiżew Apr 10 '18 at 13:06
  • this `rx = /"([^"]+)"|\S+/g;` its just for `" "` how can i make it for this kind quotes `“ ”` also – MokiNex Apr 11 '18 at 10:56
  • 1
    `rx = /["“]([^"”]+)["”]|\S+/g;` – Wiktor Stribiżew Apr 11 '18 at 10:57
  • why It doesn't work for me when I have input with whitespace `" a reference "` as an example – MokiNex Apr 16 '18 at 11:52
  • I want to add this quotes also `' '` to my `var rx = /["“']([^"”']+)["”']|\S+/g;` but its doesn't work could you explain me why? – MokiNex Apr 23 '18 at 08:49
  • the problem is when I have a simple word `reference` and `'a reference'` just the first word reference it will be highlighted https://jsfiddle.net/szj8mwxw/9/ – MokiNex Apr 23 '18 at 09:20
  • @MokiNex That has nothing to do with quotes. Your first `reference` got matched and highlighted, so `a reference` cannot match any longer. See [this demo](https://jsfiddle.net/szj8mwxw/10/). – Wiktor Stribiżew Apr 23 '18 at 09:22
  • yes, you are right so now the problem with the matching words but and sometimes the word `reference` can be in the front of the query and I'm not allowed to change the query – MokiNex Apr 23 '18 at 09:24
  • 1
    @MokiNex Not a problem, just sort `wordsTH2` by length in a descending order, see [this demo](https://jsfiddle.net/Lw6dq01g/). – Wiktor Stribiżew Apr 23 '18 at 09:27
  • this is my final method for highlighting n sequences words my method works well only in this condition I don't know what is the problem I still hours to figure out the problem https://jsfiddle.net/Lw6dq01g/2/ – MokiNex Apr 23 '18 at 12:11
  • can you explain to me how u fixed the duplicate words for highlighting? – MokiNex Apr 23 '18 at 12:21
  • 1
    I added a `.*?|` alternative to the final regex, you may replace `.` with `[^]` to also match across lines, it will match terms that are already highlighted and won't touch them since if it matches, Group 1 and 2 are undefined and if Group 1 and 2 match, a term (`$2`) will get wrapped with the new span. – Wiktor Stribiżew Apr 23 '18 at 12:23
  • thnx https://jsfiddle.net/Lw6dq01g/4/ why in this case I cant highlight `a reference` when its in the front of the phrase – MokiNex Apr 23 '18 at 15:42
  • @MokiNex I am busy right now, but I see you posted another question. Maybe your whole approach should be changed, e.g. create a regex with search words as alternations, to process a string with a single replace. – Wiktor Stribiżew Apr 23 '18 at 16:18
  • if I put whitespace in front of the word `a reference` it will be highlighted do you know why? https://jsfiddle.net/Lw6dq01g/5/ – MokiNex Apr 23 '18 at 16:50
  • @MokiNex I answered your other question. – Wiktor Stribiżew Apr 23 '18 at 18:11