I attempting to construct a JavaScript friendly regular expression that matches any strings that begin with a certain group of words (A), and if other words are included in the string they must either be within a group of words (B) or not within a group of words (C).
So given the following word groups (A), (B) and (C):
(A) Test, Sample
(B) Good, Stuff
(C) Hello, World
and given the following example strings that begin with any words in (A):
Test
Test Good
Sample Stuff
Test Hello
Sample World
Test Hello Stuff
Sample Good World
Test Other
Test Other Stuff
Sample Other World
Test Other Stuff Other
The following strings would be matched:
Test
Test Good
Sample Stuff
Test Other Stuff
Test Other Stuff Other
Ideally only the words in group A ("Test" and "Sample" in this case) would be consumed by the expression, and the rest would be handled by positive and negative lookaheads. However I can also work with all or part of a string that begins with (A) may contain (B) but does not contain (C).
I have been working on this problem for several days now, and the closest answer I have found on this website so far is:
Is there a regex to match a string that contains A but does not contain B
However the solution that is suggested there does not include the requirement for starting words to be matched singularly (as is the case in my example with the first match "Test").
The closest I have come to a solution is the following expression:
^(Test|Sample).*(?=(Good|Stuff))(?!.*(Hello|World)).*
See here for a working example:
https://regex101.com/r/nL0iE3/1
However this does not match single instances of words in (A) (e.g. "Test") and matches words in (C) when they occur before words in (B) (e.g. "Sample World Good").
I hope that makes sense, but please let me know if I can clarify anything further. I would be very grateful for any help or pointers in the right direction.