Home > OS >  Javascript regex to match a list of specific words in any order
Javascript regex to match a list of specific words in any order

Time:10-11

I have sentences like 'On the green and purple and yellow and red grass', or 'On the green and purple and yellow and red and yellow and green and green grass', or 'On the green grass', and I want to match them to a regex and extract the specific words ('green', 'purple', 'yellow', 'red') from them. I tried the following:

const regex = /On the (green|purple|yellow|red)( and (green|purple|yellow|red))* grass/

So, like, match the first word (which should always be there), and then ' and [..]' any number of times.

So then I do this: 'On the green and green and green grass'.match(regex)

But the results are:

'On the green and green and green grass',
'green',
' and green',
'green',

And for On the green and green and green and purple and purple and yellow grass

it's

'On the green and green and green and purple and purple and yellow grass',
'green',
' and yellow',
'yellow',

So this gets the first word and the last word. How do I get all the words? That is, from the first example I want to extract [green, green, green], and from the second [green, green, green, purple, purple, yellow]. I don't want to match/extract anything from On the green and red and black grass because black isn't in the list of allowed words. In addition, the sentence is specific: it starts with On the and ends with grass. I was wondering if it's possible to check all of this with a single regex.

CodePudding user response:

Your question isn't really clear but assuming you want to extract the colours, simply grouping them along with the OR boolean and a global flag should work fine:

const regex = /(green|red|purple|yellow)/g;

const str1 = "On the red and green and green and green and purple and purple and yellow grass";
const str2 = "On the green and green and green grass";

console.log(str1.match(regex));
console.log(str2.match(regex));

CodePudding user response:

If you only want to match the colours in that particular sentence, you can avoid the issue of only getting the last match of the capture group by using lookarounds instead, for example:

const text = 'On the green and green and green and purple and purple and yellow grass';
const colors = '(?:green|purple|yellow|red)';

const regex = new RegExp(
  `(?<=On the (?:${colors} and )*)${colors}(?=(?: and ${colors})* grass)`,
'g');

console.log(text.match(regex));

  • Related