A. To Match ANSWER and ME separately (see demo)
\b(?!DMV|OK)[A-Z]+\b
This is the same as (?!DMV|OK)\b[A-Z]+\b (you can place the opening boundary \b before or after the negative lookahead).
Using Javascript:
var regex = /\b(?!DMV|OK)[A-Z]+\b/;
var match = regex.exec(string);
if (match != null) {
result = match[0];
}
How it works
- The
[A-Z]+ matches an upper-case word
- The boundaries
\b on each side of the uppercase letters ensure we have a whole word, not some letters embedded in another word
- The negative lookahead
(?!DMV|OK) before the word ensures that the word we match is neither OK nor DMV
B. To Match ANSWER ME together (see demo)
(?:\b(?!DMV|OK)[A-Z]+\b\s*)+
Again, you can move the opening \b if you prefer it before the [A-Z]+
In JavaScript:
var regex = /(?:\b(?!DMV|OK)[A-Z]+\b\s*)+/;
var match = regex.exec(string);
if (match != null) {
result = match[0];
}
How it works
- The
(?:...)+ non-capturing group matches one or more upper-case words [A-Z]+ followed by optional whitespace characters \s*
- The boundaries
\b on each side of the uppercase letters ensure we have a whole word, not some letters embedded in another word
- The negative lookahead
(?!DMV|OK) before each word ensures that the word we match is neither OK nor DMV
[^OK]matches anything that's not anOor aK. The same thing with the DMV one. Do you just want what's after the?? If so, that's much easier and you should ask that.