Home > Software engineering >  Print Sentences of Array, If They Contain the Given Word
Print Sentences of Array, If They Contain the Given Word

Time:10-28

I hope it won't be difficult to figure out these symbols. I need to print all the sentences from sentences[] which have the same index as the sentences of sentencesRemovedPuncts[] having the word "մի՛" (in this case, in general any word) in them.

var sentences = ["մի՛ գնար՝ ի տուն։", "մի անգամ, գեթ մի․․․։", "Զ՞ինչ մի՛թե է այս։"]
var sentencesRemovedPuncts = ["մի՛ գնար ի տուն", "մի անգամ գեթ մի", "Զինչ միթե է այս"] 
let word = "մի՛"
let key = new RegExp(`\b${word}\b`)
for (let i = 0; i < sentencesRemovedPuncts.length; i  ) {
  if (key.test(sentencesRemovedPuncts[i])) {
    console.log(sentences[i])
  }
}

But my code doesn't output anything.

CodePudding user response:

Please find the below snippet to find if the word exists in a string

// (String).split(" ").indexOf(word) !== -1
sentencesRemovedPuncts[i].split(" ").indexOf(word) !== -1

Below is the RegExp to find unicode sentences

let word = "մի՛"
let bound = `[\\s\\r\\n]{1}`;
let key = new RegExp(`^(${word}${bound})|(${bound}${word}${bound})|(${bound}մի՛)$`)
...
key.test(sentencesRemovedPuncts[i])
  • Related