Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Regular Expressions Lookarounds

A Regular Expression (regex) is a sequence of characters that define a pattern that allows you to search, match, locate, replace, manipulate, and manage text. I explained the basics of Regular Expressions in an earlier article: What is a Regular Expression?

If you need to match a pattern only when it comes after another pattern (called a lookbehind) or only when it comes before another pattern (called a lookahead) you need these operations, cumulatively called lookarounds.

Lookbehind Positive

In the example below, we want to match the price in the string "I purchased 24 item number 6602 for $12 each."

<script>
let strTarget = "I purchased 24 item number 6602 for $12 each.";
let regexp = /(?<=\$)\d+/;
let res = strTarget.match(regexp);
alert(res);
</script>

The syntax for lookbehind positive is shown below.

(?<=B)A

It means match expression A, but only where it is preceded by expression B.

In this example expression A is one or more numerical digits, but they are matched only if preceded by expression B, which is a $ sign. Note that the dollar sign is escaped, \$, to make sure it's not confused with the $ end of string control character.

This example displays the number 12 in a message box.

Lookbehind Negative

In the example below, we want to match the quantity in the string "I purchased 24 item number 6602 for $12 each."

<script>
let strTarget = "At $9 each, I purchased 24 item number 6602.";
let regexp = /(?<!\$)\d+/;
let res = strTarget.match(regexp);
alert(res);
</script>

The syntax for lookbehind negative is shown below.

(?<!B)A

It means match expression A, only where it is NOT preceded by expression B.

In this example expression A is one or more numerical digits, but they are matched only if NOT preceded by expression B. Expression B is a $ sign followed by a numerical digit. So it will find the first numerical match which is NOT preceded by a $ sign.

This example displays the number 24 in a message box.

Lookahead Positive

In the example below, we want to match the item number in the string "I purchased 24 item number 6602 for $12 each."

<script>
let strTarget = "I purchased 24 item number 6602 for $12 each.";
let regexp = /\d+(?=\sfor)/;
let res = strTarget.match(regexp);
alert(res);
</script>

The syntax for lookahead positive is shown below.

A(?=B)

It means match expression A only where expression B follows.

In this example expression A is one or more numerical digits, but they are matched only if followed by expression B. Expression B is a space followed by the word "for". So it will NOT match the quantity 24, it will match the number 6602.

Note that expression A must be followed immediately by expression B, if this wasn't true, the quantity number would be the match because it is also followed by expression B, in fact it would be the first match.

This example displays the number 6602 in a message box.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2019 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268