1

Hi Stack Overflow,

I have this three regexp:
var regex = new RegExp(/[A-Z]+/g); // This is to select only word with CAPS
var regexOk = new RegExp(/[^OK]+/g); // This is to select anything beside OK
var regexDmv = new RegExp(/[^DMV]+/g); // This is to select anything beside DMV

// And a variable: 
var string = "Let's go to the DMV tomorrow, is that OK? ANSWER ME!";

Basically what I want to do is to search this string, so that it returns ['ANSWER', 'ME']

Can anyone give me suggestion?

1
  • 1
    [^OK] matches anything that's not an O or a K. 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. Commented Jun 17, 2014 at 2:31

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Good, but the answer could be improved by adding a bit of explanation as to what's going on here. There's some saying about a man and a fish...
Thank you so much. I am still trying to understand your explanation. But the demo is really helpful.
Thank you, definitely helpful. How do I refresh the question?
@user122652 If you are looking at it now, you are probably looking at the latest version. :) By "refresh" I meant hitting the F5 key on your keyboard, or something like that, to reload the page.
2

There is no NOT string operator in regex. [^OK] means any character besides O and K. You can just do

/ANSWER|ME/

1 Comment

I am actually trying to find words that have all CAPS beside OK and DMV

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.