regex - Regular expression exclude one word -


i want write regular expression find strings looks "animals.* " on '*' place can symbols, not 'elephant'.

result should be:

input: animals.fox => result: animals.fox

input: animals.kitten => result: animals.kitten

but

input: animals.elephant => result: none

i write regular expression, not work properly. how fix it?

animals\.(?!elephant) 

it seems problem matching word after .. lookahead used zero-width assertion not put matched chars output, checks if text right of current location not match pattern. if matches, match failed. else, returns true. so, need consume word after ..

add [a-za-z]+ or \w+ @ end of pattern (depending on need match, letters or letter/digits/underscore):

animals\.(?!elephant)\w+ 

see regex demo


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -