-2

I have some troubles with regex, until now, I had no problem except for the following one :

I have 2 strings, I want to match with one but not the second one which contains a specific word.

var text1 = "The sun is yellow and the sky is blue";
var text2 = "The sun is yellow and the clouds are white"; 

It's really basic for the example but my regex was like that before :

var regex = /sun/g;

So this was ok for the text1 BUT now I want to return false the match if the string contains "clouds" So text1 would be TRUE but not text2

I tried with (?!clouds) but I'm probably doing it wrong. It's pretty hard to use regex at this level. So I hope you could help me.

Thank you

Genki
  • 11
  • 5

1 Answers1

3

Something like this would do it:

^(?!.*\bclouds\b)(?=.*\bsun\b).*$

https://regex101.com/r/TYZHwS/1

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77