Home > database >  RegEx How to match text between Multiple words regardless of their order?
RegEx How to match text between Multiple words regardless of their order?

Time:10-21

For a given set of words, I would like to get all the words in between inclusively. For example : words = ['word1', 'word2', 'word3']

Lorem ipsum dolor sit word2, consectetur adipiscing elit. word3 tristique in dolor vel consequat. Nulla tincidunt suscipit molestie. Suspendisse mauris turpis, ultricies pulvinar facilisis word1, vulputate sit amet . Donec cursus odio ut ipsum rutrum faucibus. Ut accumsan arcu ac ex scelerisque, ac sodales metus dictum. Nam efficitur velit sed lorem pharetra commodo. Morbi velit massa, feugiat nec ligula nec, finibus tincidunt nulla. Nulla a suscipit elit. Proin in nibh nec ipsum eleifend tempor. .

The words in Italic should be a match.

CodePudding user response:

Seems like match can do what you need.

This:

"hello from somewhere, this is a nice place".match(/(hello|this)/gi)

outputs:

[ 'hello', 'this' ]

CodePudding user response:

It's possible using lookahead and lookbehind assertions:

'…your string…'.match(/(?<=word1|word2|word3).*?(?=word1|word2|word3)/g)

output:

[
  ", consectetur adipiscing elit. ",
  " tristique in dolor vel consequat. Nulla tincidunt suscipit molestie. Suspendisse mauris turpis, ultricies pulvinar facilisis "
]

edit: Lookbehind assertions are currently not supported by Safari.

By using only lookahead assertions like this:

/(?!word1|word2|word3)\b.*?(?=word1|word2|word3)/g

the part before the first keyword would also be included (if present), but you could remove it afterwards from the result like this (in JavaScript):

('x'   '…your string…')
  .match(/(?!word1|word2|word3)\b.*?(?=word1|word2|word3)/g)
  .slice(1)

outputs the desired result:

[
  ", consectetur adipiscing elit. ",
  " tristique in dolor vel consequat. Nulla tincidunt suscipit molestie. Suspendisse mauris turpis, ultricies pulvinar facilisis "
]
  • Related