117

How do I use a javascript regular expression to check a string that does not match certain words?

For example, I want a function that, when passed a string that contains either abc or def, returns false.

'abcd' -> false

'cdef' -> false

'bcd' -> true

EDIT

Preferably, I want a regular expression as simple as something like, [^abc], but it does not deliver the result expected as I need consecutive letters.

eg. I want myregex

if ( myregex.test('bcd') ) alert('the string does not contain abc or def');

The statement myregex.test('bcd') is evaluated to true.

Soviut
  • 88,194
  • 49
  • 192
  • 260
bxx
  • 1,701
  • 2
  • 12
  • 16

6 Answers6

178

This is what you are looking for:

^((?!(abc|def)).)*$

The ?! part is called a negative lookahead assertion. It means "not followed by".

The explanation is here: Regular expression to match a line that doesn't contain a word

Matthias
  • 13,607
  • 9
  • 44
  • 60
ssgao
  • 5,151
  • 6
  • 36
  • 52
  • 1
    This is the answer I expect! Thanks. I need a regular expression rather than a function. My question was edited and answers came up to answer the new version of my question. This is why I used an "EDIT" part to avoid confusion. – bxx Mar 21 '13 at 03:53
  • 2
    Is there an answer that doesn't match a whole word? Your example "abc", "babc" and "abcd" all fail and where as "xyz" passes. I need "abc" to fail but "abcd" to pass. Removing the `.` and `*` don't seem to work – gman Jan 02 '17 at 17:15
  • @gman : it's unclear whether you want to capture words (like capture 'abcd' but not 'abc') or if like here, you just want to match an entire text not containing 'abc' or 'def'. "Capturing a word" and "capture everything if a word is not there" are two whole different things. However, if you want to go the route of the negative lookahead, here is your regEx : `^((?!((?=\b)abc(?=\b)|(?=\b)def(?=\b))).)*$`. It will capture the whole text (sort of, caution last char) if it doesn't contain any of the words 'abc' or 'def'. So, it will match 'abcd', but not 'abc?' because '?' is not part of the word. – Karl Stephen Sep 15 '22 at 20:35
  • Sorry, I meant `^((?!(\babc\b|\bdef\b)).)*$`. To capture 'abcd' but discard the whole text if it contains 'abc' or 'def', better check the existence of 'abc' or 'def' first, and if none, proceed on matching words containing 'abc' or 'def'... Buuuuut.. does that have any useful purpose ?? – Karl Stephen Sep 15 '22 at 20:41
23
if (!s.match(/abc|def/g)) {
    alert("match");
}
else {
    alert("no match");
}
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
7

Here's a clean solution:

function test(str){
    //Note: should be /(abc)|(def)/i if you want it case insensitive
    var pattern = /(abc)|(def)/;
    return !str.match(pattern);
}
NoBrainer
  • 5,853
  • 1
  • 27
  • 27
2
function test(string) {
    return ! string.match(/abc|def/);
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
2
function doesNotContainAbcOrDef(x) {
    return (x.match('abc') || x.match('def')) === null;
}
Bemmu
  • 17,849
  • 16
  • 76
  • 93
1

This can be done in 2 ways:

if (str.match(/abc|def/)) {
                       ...
                    }


if (/abc|def/.test(str)) {
                        ....
                    } 
Girish Gupta
  • 1,241
  • 13
  • 27