**Note:** To understand the following, I expect you to know how regex lookahead works. If you don’t, read about it first and return here after you’re done.

I was quite excited to discover this, but to my dismay, Steven Levithan assured me it’s actually well known. However, I felt it’s so useful and underdocumented (the only references to the technique I could find was several StackOverflow replies) that I decided to blog about it anyway.

If you’ve been using regular expressions for a while, you surely have stumbled on a variation of the following problems:

**Intersection**: “I want to match something that matches pattern A AND pattern B”

*Example: A password of at least 6 characters that contains at least one digit, at least one letter and at least one symbol***Subtraction**: “I want to match something that matches pattern A but NOT pattern B”

*Example:**Match any integer that is not divisible by 50***Negation**: “I want to match anything that does NOT match pattern A”

*Example: Match anything that does NOT contain the string “foo”*

Even though in ECMAScript we can use the caret (^) to negate a character class, we cannot negate anything else. Furthermore, even though we have the pipe character to mean OR, we have nothing that means AND. And of course, we have nothing that means “except” (subtraction). All these are fairly easy to do for single characters, through character classes, but not for entire sequences.

However, we can mimic all three operations by taking advantage of the fact that lookahead is zero length and therefore does not advance the matching position. We can just keep on matching to our heart’s content after it, and it will be matched against the same substring, since the lookahead itself consumed no characters. For a simple example, the regex `/^(?!cat)\w{3}$/`

will match any 3 letter word that is NOT “cat”. This was a very simple example of **subtraction**. Similarly, the solution to the subtraction problem above would look like `/^(?!\d+[50]0)\d+$/`

.

For **intersection** (AND), we can just chain multiple positive lookaheads, and put the last pattern as the one that actually captures (if everything is within a lookahead, you’ll still get the same boolean result, but not the right matches). For example, the solution to the password problem above would look like `/^(?=.*\d)(?=.*[a-z])(?=.*[\W_]).{6,}$/i`

. Note that if you want to support IE8, you have to take this bug into account and modify the pattern accordingly.

**Negation** is the simplest: We just want a negative lookahead and a .+ to match anything (as long as it passes the lookahead test). For example, the solution to the negation problem above would look like `/^(?!.*foo).+$/`

. Admittedly, negation is also the least useful on its own.

There are caveats to this technique, usually related to what actually matches in the end (make sure your actual capturing pattern, outside the lookaheads, captures the entire thing you’re interested in!), but it’s fairly simple for just testing whether something matches.

Steven Levithan took lookahead hacking to the next level, by using something similar to mimic conditionals and atomic groups. Mind = blown.

Pingback: try here()

Pingback: angara fahise()

Pingback: bursa orospu()

Pingback: jumpstartyourwealthgenereview.org/()

Pingback: http://www.opulenceforlifebonus.com/reviews/()

Pingback: slipandfalllawyers.net()

Pingback: Term Life Insurance()

Pingback: tube advertising()

Pingback: buy legal steroids online()

Pingback: tumblr blog()

Pingback: blogspot.com()

Pingback: hampton bay website()

Pingback: https://tribot.org()

Pingback: Dragon Ball Super Episode 46 English Sub()

Pingback: cdt.edu.vn()

Pingback: Murat Güven Düsseldorf()

Pingback: xmt85c4wx5ctwxw3tcerthve56()

Pingback: http://falschgeldkaufen.blogspot.com/2017/01/wo-kann-ich-falschgeld-kaufen.html()

Pingback: end stage alcoholism()

Pingback: دانلود قسمت هشتم شهرزاد دوم()

Pingback: دانلود شهرزاد قسمت هفتم فصل دوم()

Pingback: دانلود شهرزاد قسمت دوازدهم فصل دوم()

Pingback: دانلود شهرزاد قسمت نهم فصل دوم()

Pingback: قیمت پنجره()

Pingback: porn()

Pingback: دانلود قسمت ششم فصل دوم شهرزاد()

Pingback: دانلود قسمت دهم فصل دوم شهرزاد()

Pingback: دانلود قسمت 17 عاشقانه()

Pingback: دانلود قسمت 12 عاشقانه()

Pingback: دانلود قسمت سیزده عاشقانه()

Pingback: Karter Foxx()

Pingback: August Ames()

Pingback: Jada Stevens()

Pingback: دانلود قسمت هفتم فصل دوم شهرزاد()

Pingback: hampton bay ceiling fan blog()

Pingback: April O'Neil()

Pingback: lesbian porn xxx()

Pingback: casinos online in kentucky()

Pingback: پنجره()

Pingback: دانلود قسمت اول شهرزاد دوم()

Pingback: دانلود قسمت پنجم شهرزاد دوم()

Pingback: دانلود قسمت سوم شهرزاد دوم()

Pingback: دانلود قسمت 11 شهرزاد 2()

Pingback: how to bypass windows 7 password()

Pingback: titleist 917()

Pingback: videos porno()

Pingback: porno gratis()

Pingback: porno()

Pingback: porno()

Pingback: videos porno()

Pingback: porno()

Pingback: porno()

Pingback: porno gratis()

Pingback: porno gratis()

Pingback: videos porno()

Pingback: videos porno()

Pingback: porno gratis()

Pingback: porno gratis()

Pingback: porno gratis()

Pingback: قیمت پنجره دوجداره در ایران()

Pingback: آموزش پنجره دوجداره()

Pingback: libra man in love signs()

Pingback: فروش پنجره دو سه جداره()

Pingback: پنجره دوجداره ویستابست()

Pingback: mysql hosting()

Pingback: Acne Facial Treatments Pembroke Park()

Pingback: Facial Spa Services Sea Ranch Lakes()

Pingback: Facial Spa Services Tamarac()

Pingback: دانلود موزیک()

Pingback: دانلود قسمت ششم شهرزاد()