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 Answers
Reset to default 10I 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)?.+/
本文标签:
版权声明:本文标题:regex - Defining a JavaScript regular expression that matches anything except a particular string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739335432a2158683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
!string.match(/^dog|cat$/)
– Linus Kleen Commented Jan 13, 2012 at 15:49str != "dog"
? – Wayne Commented Jan 13, 2012 at 15:56