Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
738 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...