Regular expression to find two strings anywhere in input

后端 未结 7 519
情话喂你
情话喂你 2020-11-30 20:34

How do I write a regular expression to match two given strings, at any position in the string?

For example, if I am searching for cat and mat

7条回答
  •  独厮守ぢ
    2020-11-30 20:56

    If you absolutely need to only use one regex then

    /(?=.*?(string1))(?=.*?(string2))/is
    

    i modifier = case-insensitive

    .*? Lazy evaluation for any character (matches as few as possible)

    ?= for Positive LookAhead it has to match somewhere

    s modifier = .(period) also accepts line breaks

提交回复
热议问题