-1

The regex /hello|world/ can match a string that contains hello OR world.

I want to match only strings that contain hello AND world in no particular order. I thought something like this could work but it didn't:

/hello&world/

The following should be matched:

hello world hello dear world a hello b world c world hello

These should not match:

world hello a world b asdf

How do I write this regex so it matches strings that contain multiple words?

swift-lynx
  • 3,219
  • 3
  • 26
  • 45

1 Answers1

0

The following expression works on your strings:

/hello.*world|world.*hello/

Basically, it tests if a string contains ("hello" then "world") OR ("world" then "hello").
Note that this approach is not practical if you have many words.

Guillaume Adam
  • 191
  • 2
  • 10