Regex AND operator
Asked Answered
J

4

78

Based on this answer

Regular Expressions: Is there an AND operator?

I tried the following on http://regexpal.com/ but was unable to get it to work. What am missing? Does javascript not support it?

Regex: (?=foo)(?=baz)

String: foo,bar,baz

Jemappes answered 14/6, 2010 at 22:13 Comment(0)
L
119

It is impossible for both (?=foo) and (?=baz) to match at the same time. It would require the next character to be both f and b simultaneously which is impossible.

Perhaps you want this instead:

(?=.*foo)(?=.*baz)

This says that foo must appear anywhere and baz must appear anywhere, not necessarily in that order and possibly overlapping (although overlapping is not possible in this specific case because the letters themselves don't overlap).

Lanalanae answered 14/6, 2010 at 22:15 Comment(3)
I just tried it on regexpal.com. No cigar. Maybe this is a bug with regexpal?Jemappes
@ghee22: The regular expression works fine but using regexpal probably isn't the best way to test it because there is no visible feedback of whether the match succeeds or fails (it is a zero-width match). Try running it in a Javascript console instead or add .* to the end of the expression.Lanalanae
This definitely works in Javascript but not in regexpal.comBetty
F
28

Example of a Boolean (AND) plus Wildcard search, which I'm using inside a javascript Autocomplete plugin:

String to match: "my word"

String to search: "I'm searching for my funny words inside this text"

You need the following regex: /^(?=.*my)(?=.*word).*$/im

Explaining:

^ assert position at start of a line

?= Positive Lookahead

.* matches any character (except newline)

() Groups

$ assert position at end of a line

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Test the Regex here: https://regex101.com/r/iS5jJ3/1

So, you can create a javascript function that:

  1. Replace regex reserved characters to avoid errors
  2. Split your string at spaces
  3. Encapsulate your words inside regex groups
  4. Create a regex pattern
  5. Execute the regex match

Example:

function fullTextCompare(myWords, toMatch){
    //Replace regex reserved characters
    myWords=myWords.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    //Split your string at spaces
    arrWords = myWords.split(" ");
    //Encapsulate your words inside regex groups
    arrWords = arrWords.map(function( n ) {
        return ["(?=.*"+n+")"];
    });
    //Create a regex pattern
    sRegex = new RegExp("^"+arrWords.join("")+".*$","im");
    //Execute the regex match
    return(toMatch.match(sRegex)===null?false:true);
}

//Using it:
console.log(
    fullTextCompare("my word","I'm searching for my funny words inside this text")
);

//Wildcards:
console.log(
    fullTextCompare("y wo","I'm searching for my funny words inside this text")
);
Fistula answered 8/6, 2016 at 3:4 Comment(0)
D
3

Maybe you are looking for something like this. If you want to select the complete line when it contains both "foo" and "baz" at the same time, this RegEx will comply that:

.*(foo)+.*(baz)+|.*(baz)+.*(foo)+.*

Delict answered 5/4, 2016 at 11:27 Comment(0)
M
-1

Maybe just an OR operator | could be enough for your problem:

String: foo,bar,baz

Regex: (foo)|(baz)

Result: ["foo", "baz"]

Mould answered 24/2, 2016 at 15:28 Comment(2)
The question is about AND logic. OR logic with alternation (|) will also match if only one or the other words is present, which is not the intent - both must be present.Oceanography
@Oceanography you're absolutely right. I only provided one alternative that maybe could be useful, too.Mould

© 2022 - 2024 — McMap. All rights reserved.