admin管理员组

文章数量:1221774

Ok, I'm feeling pretty thick right now. Basically, I want to define a JavaScript regular expression that will match anything except precisely a particular string. So say I have the string

"dog"

or

"cat"

I want a single standalone regular expression such that it will match the string

"dogsled"

or the string

"cattle"

Just not "dog" or "cat" on its own. I've tried this, which basically says, ignore anything beginning with "cat" or "dog", which is not exactly what I need...

var pattern= /^(?!dog|cat).+/

pattern.test("cat") // false, as expected
pattern.test("dog") // false, as expected
pattern.test("bananananana") // true
pattern.test("dogsled") // false, but the regexp I want would return true

This has to be simple.... thanks!

Edit Just to clarify, I don't want to do any negation of return values to get the result I want- the regex should return false for "dog" and true for "dogsled" or false for "cat" and true for "cattle"

Ok, I'm feeling pretty thick right now. Basically, I want to define a JavaScript regular expression that will match anything except precisely a particular string. So say I have the string

"dog"

or

"cat"

I want a single standalone regular expression such that it will match the string

"dogsled"

or the string

"cattle"

Just not "dog" or "cat" on its own. I've tried this, which basically says, ignore anything beginning with "cat" or "dog", which is not exactly what I need...

var pattern= /^(?!dog|cat).+/

pattern.test("cat") // false, as expected
pattern.test("dog") // false, as expected
pattern.test("bananananana") // true
pattern.test("dogsled") // false, but the regexp I want would return true

This has to be simple.... thanks!

Edit Just to clarify, I don't want to do any negation of return values to get the result I want- the regex should return false for "dog" and true for "dogsled" or false for "cat" and true for "cattle"

Share Improve this question edited Jan 13, 2012 at 16:06 VLostBoy asked Jan 13, 2012 at 15:45 VLostBoyVLostBoy 4,1943 gold badges25 silver badges31 bronze badges 5
  • 3 +1 for actually giving the regex a go. So many regex questions are just "I can has code" style. – Matt Commented Jan 13, 2012 at 15:47
  • 5 Why not simply negate? !string.match(/^dog|cat$/) – Linus Kleen Commented Jan 13, 2012 at 15:49
  • @LinusKleen ha ha ha great username for a regex question :-) – Pointy Commented Jan 13, 2012 at 15:51
  • 1 Wait, why not str != "dog"? – Wayne Commented Jan 13, 2012 at 15:56
  • @LinusKleen- thanks, but I'm explicitly trying to avoid negation of the return value to get what I want- that is, I want a pure regex that will return true or false to the case above – VLostBoy Commented Jan 13, 2012 at 15:58
Add a comment  | 

3 Answers 3

Reset to default 10

I would opt for simply negating a match as noted in Linus Kleen's answer. However, if you absolutely must do it all in regex (or for the learning experience or something) then I think the following should work:

^((dog|cat(?!$)).+|(?!dog|cat).+)$

This uses two negative lookaheads in an alternation. The first says "Match cat or dog if it is not followed by the end of string character ($), followed by anything else". This matches dogsled and cats and things of that nature.

The second half of the alternation says "Make sure the beginning of the string (^) is not followed by dog or cat (so it doesn't start with either of those), then match anything". This gets you any word that doesn't begin with cat or dog (like banana).

Here's an example on regexpal.

I seem to recall negative look-ahead assertions ((?! ... )) to be not fully supported in all Javascript interpreters. A simple negation on the result of the regex should suffice as well, no?

var pattern = /^cat|dog$/;

!pattern.test("cat");           // => false
!pattern.test("dog");           // => false
!pattern.test("bananananana");  // => true
!pattern.test("dogsled");       // => true

i think this should do the trick:

/^(dog|cat)?.+/

本文标签: