Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex: string contains this, but not that

I'm trying to put together a regex pattern that matches a string that does contain the word "front" and does NOT contain the word "square". I have can accomplish this individually, but am having trouble putting them together.

front=YES

 ^((?=front).)*$

square=NO

 ^((?!square).)*$

However, how to I combine these into as single regex expression?

like image 943
Andrew Hall Avatar asked Apr 09 '26 08:04

Andrew Hall


1 Answers

You can use just a single negative lookahead for this:

/^(?!.*square).*front/

RegEx Demo

RegEx Details:

  • ^: Start
  • (?!.*square) is negative lookahead to assert a failure if text square is present anywhere in input starting from the start position
  • .*front will match front anywhere in input
like image 81
anubhava Avatar answered Apr 10 '26 22:04

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!