javascript regex: string contains this, but not that -


i'm trying put regex pattern matches string contain word "front" , not contain word "square". have can accomplish individually, having trouble putting them together.

front=yes

 ^((?=front).)*$ 

square=no

 ^((?!square).)*$ 

however, how combine these single regex expression?

you can use single negative lookahead this:

/^(?!.*square).*front/ 
  • (?!.*square) negative lookahead assert failure if square present anywhere in input
  • ^.*front match front anywhere in input

Comments