Home > Mobile >  How to create a regular expression which for each sentence proves the existence of several sentence
How to create a regular expression which for each sentence proves the existence of several sentence

Time:04-08

The text I want to scan for is: "Why have you come here today?" (key words being "why" "you" "come" "here" "today")

I'd like to build a regex expression that can also scan for similar questions like:

  • "What's brought you here today?" (key words being "what's", "brought", "you", "today")
  • "What is the reason for coming in today?" (key words being "what" "reason", "coming", "today")
  • "Is there a reason you are here this morning?" (key words being "is" "reason", "here", "morning")
  • Why are you here today? (key words being "why", "you", "here", "today")?

So something like: /keywords from first sentence | keywords from second sentence | keywords from third sentence | etc... /ig

And if the above regular expression is true (using .test), then the user will be alerted something.

I've tried using the following: /(?=.*\bwhat's\b)(?=.*\bbrought\b)(?=.*\byou\b)(?=.*\btoday\b)|(?=.*\bwhy\b)(?=.*\byou\b)(?=.*\bhere\b)(?=.*\btoday\b)/ig. And so it doesn't matter which question is asked because the same function will be executed regardless (all I need is the key words to match!)

I'd like to build a regular expression that can scan for key words from all these phrases and then, if the key words are matched, a function will be executed. If anyone has any suggestions on topics/readings for creating such a regular expression that would be great!

CodePudding user response:

@PeterSeliger would you have any advice on where I can look to get started on creating such a solution? – sb2021

1/2 A working solution needs a regex for splitting a multiline string into an array of (line) strings. One also needs an array of arrays where each nested array holds the keywords of a line/sentence.

2/3 ... Then one wants to check for the array of new lines whether every single line matches the keyword criteria. The callback function, which has to return a boolean value, then needs to check whether every line related keyword is part of the currently processed line. And the callback function of this task will utilize a dynamically created regex ...

3/3 ... which uses the i flag in order to test the existence of a keyword in a case insensitive way (ignore case) ... because of one of the OP's use cases where one runs into comparing "What's" versus "what's"

const sampleMultilineData =
`What's brought you here today?
What is the reason for coming in today?
Is there a reason you are here this morning?
Why are you here today?`;

const listOfKeywordLists = [
  ["what's", "brought", "you", "today"],
  ["what", "reason", "coming", "today"],
  ["is", "reason", "here", "morning"],
  ["why", "you", "here", "today"],
];

const isEveryLineContainsAllOfItsKeywords = sampleMultilineData
  // create array of separte lines
  // from the multiline string data.
  .split(/\n/)
  // wheter every single line matches the keyword criteria.
  .every((line, lineIndex) =>

    // access the line related keyword list via `lineIndex`.
    listOfKeywordLists[lineIndex]
      // wheter a line contains every keyword
      // with word boundaries (`\b`) to the keyword
      // in a case insensitive (`i` flag) way.
      .every(keyword =>
        RegExp(`\\b${ keyword }\\b`, 'i').test(line)
      )

      // // works case sensitive, thus the result is `false`.
      //.every(keyword => line.includes(keyword))
  );

console.log({ isEveryLineContainsAllOfItsKeywords });
.as-console-wrapper { min-height: 100%!important; top: 0; }

  • Related