0

Regex for matching all words in any order and if any word is missing in the string, it will not return any result.

e.g.

str = "You’re ready to ask a programming-related question and this form will help guide you through the process.

If our requirement is to find the match if str contains both words 'ready' & 'question` in any order, then the pattern would be:

/\bready\b.*\bquestion\b/gi or /\bready\b.*\bquestion\b|\bquestion\b.*\bready\b/gi.

But let's say we need to find a result that contains another word like 'post' along with 'ready' & 'question' then the pattern will not return the match since the word 'post' is not present in the 'str'.

the pattern I was trying

str = 'Then Lord of the Rings: The Return of the King' pattern = /(\b(the|king|of)\b)/gi

  • Works for most engines `(?si)(?:.*?\b(?:(?!\1)(the)|(?!\2)(king)|(?!\3)(of))\b){3}` https://regex101.com/r/0tsIxo/1 https://regex101.com/r/wVt5BE/1 https://regex101.com/r/8EsstB/1 https://regex101.com/r/qEnAKw/1 JavaScript is still archiac – sln May 22 '23 at 23:31
  • A followup note on the archaic remnants of the JavaScript engine. It does not recognize, nor will it consider an assertion or a usage of a back reference to a group that has yet to be defined. Instead of considering the proposition reference as _False_ it decrees the reference forever invalid. This happens in the regex compile stage. Every modern engine, including Dot-Net has fixed this extremely deficient stain on the Regular Expression Language. JavaScript is indeed a dinosaur and remains so. – sln May 23 '23 at 21:18

0 Answers0