Javatpoint Logo
Javatpoint Logo

Javascript regexp Lookahead

Lookahead patterns enable JavaScript to move forward through the string to search for specified patterns. Lookaround is a combined expression of Lookahead and Lookbehind. We can only capture a specific group using lookaheads if they arrive before another group of characters. This is useful when we wish to find various patterns within the same string.

Syntax

The following syntax shows the positive Javascript regexp Lookahead

The following syntax shows the negative Javascript regexp Lookahead

Explanation

There are two different kinds of lookaheads, which are as follows:

  • Positive lookaheads: It will determine whether a specific element is included in the search pattern but won't match it. A positive lookahead is produced by enclosing a certain pattern between (?= and).
  • Negative lookaheads: It checks whether a specific element is absent from the search pattern. Enclosing a specific pattern between (?! and) results in a negative lookahead.

Examples

The following examples show the positive and negative lookahead of the regular expression.

Example1:

In the example, the "butter" matches the fly or milk using the positive lookahead expression. The first value comes before the last value then the output shows the true value. If the first value does not match with last value, then the output shows a false value.

Output

The following image shows the Boolean values as an output.

Javascript regexp Lookahead

Example2:

In the example, the "butter" matches with the fly or milk using the positive lookahead expression. We can see the regular expression with the lookahead pattern to get the required value. The $1 is essential to get the after value from the pattern.

Output

The following image shows the data values as an output.

Javascript regexp Lookahead

Example3:

In the example, the "butter" matches with the fly or milk using the negative lookahead expression. The first value does not come before the last value. Then the output shows the true value. If the first value does not match with last value, then the output shows a false value.

Output

The following image shows the Boolean values as an output.

Javascript regexp Lookahead

Example4:

In the example, the "butter" matches with the fly or milk using the negative lookahead expression. We can see the regular expression with the negative lookahead pattern to get output. This example shows the required value using $1. It is essential to get unavailable after the value from the pattern.

Output

The following image shows the Boolean values as an output.

Javascript regexp Lookahead

Example5:

In the example, the lookahead regexp uses to get the available values from pattern values. We can use the console tab, alert, and other output tabs. We can work with the match function through the lookahead function. Here, the output shows the values that come before the input value.

Output

The following image shows the Boolean values as an output.

Javascript regexp Lookahead

Example6:

In the example, the negative lookahead regexp uses to get the available values from pattern values.

Output

The following image shows the Boolean values as an output.

Javascript regexp Lookahead

Supported Browsers

The following browsers support the javascript lookbehind expression.

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari

Conclusion

The javascript lookahead expression is used to search the required keyword in the string value. This expression uses for pattern recognization and operation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA