What is a regular expression pattern for rule 'doesn't contain a word W'? [closed]

StackOverflow https://stackoverflow.com/questions/19094314

  •  29-06-2022
  •  | 
  •  

Question

I want to test the web-element style: it shouldn't contain words "disabled" or "inactive".

Était-ce utile?

La solution 2

Do you really need a "negative" regex? Why not create a regex that tests that it contains "disabled" or "inactive", and test for it to be false?

'status: inactive' !~ /(disabled|inactive)/

Autres conseils

Using negative lookahead:

>> 'status: inactive ' =~ /(?!.*(disabled|inactive))^/
=> nil
>> 'status: disabled' =~ /(?!.*(disabled|inactive))^/
=> nil
>> 'status: enabled' =~ /(?!.*(disabled|inactive))^/
=> 0
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top