views:

81

answers:

1
+2  Q: 

Regex AND operator

Based on this answer

http://stackoverflow.com/questions/469913/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

+5  A: 

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 bar must appear anywhere, not necessarily in that order and possibly overlapping (although overlapping is not possible in this specific case).

Mark Byers
I just tried it on http://regexpal.com/. No cigar. Maybe this is a bug with regexpal?
Sorry. Nevermind, that made sense. Thank you.