I know there are logical operators such as |
"the OR operator" which can be used like this:
earth|world
I was wondering how I could check if my string contains earth AND world.
regards, alexander
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Chaining regular expressionsRegular expressions can be chained together using the pipe character (|). This allows for multiple search options to be acceptable in a single regex string.
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
If it contains earth AND world, it contains one after the other, so:
earth.*world|world.*earth
an shorter alternative (using extended regex syntax) would be:
/(?=.*earth)(?=.*world).*/
But it is not at all like an and
operator. You can only do or
because if only one of the words is included, there is no ordering involved. If you want to have them both, you need to indicate the order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With