How to exlude only certain words in regex -


i want alternate regular expression. current expression allows users input long doesn't contain word 'white' or combination of words "cat" , "dog" (either "cat" or "dog" separately a).

regex before change: /^((?!(white|cat.*dog|dog.*cat))[\s\s])*$/i 

is possible alternate regex inputs "a white tiger" valid, sole word ("white) not?

solution

what need make more efficient making lookahead run once @ beginning:

/^(?!white$|[\s\s]*(?:cat[\s\s]*dog|dog[\s\s]*cat))[\s\s]*$/i 

see regex demo ([\s\s] replaced . demo since input tested line line).

explanation

the /^((?!(white|cat.*dog|dog.*cat))[\s\s])*$/i contains anchored tempered greedy token well-known regular expression match line doesn't contain word? post. [\s\s] matches character (even newline) but not in case char first in sequence defined in negative lookahead. so, regex above matches string 1 contains either white, or cat followed 0+ chars other newline , dog, or vice versa, dog , after 0+ chars other newline, cat.

so, necessary make sure white tested in between anchors: ^(?!white$)[\s\s]*$ check.

the rest of alternatives still need checked inside, at location within string. so, [\s\s]* should put before (?:cat[\s\s]*dog|dog[\s\s]*cat) group: [\s\s]*(?:cat[\s\s]*dog|dog[\s\s]*cat). way, make sure string not have these patterns inside. note .* in lookahead checked if patterns not present on first line.

details:

  • ^ - start of string
  • (?! - negative lookahead check:
    • white$ - string can't equal white
    • | - or
    • [\s\s]*(?:cat[\s\s]*dog|dog[\s\s]*cat) - 0+ chars followed either cat , after number of chars dog or vice versa
  • ) - end of lookahead
  • [\s\s]* - 0+ chars
  • $ - end of string.

Comments