W3docs

Lookahead and Lookbehind

In this chapter, we are going to explore more about finding matches in JavaScript. Learn the convenient ways of using Lookahead and Lookbehind in practice.

Understanding the advanced topics of JavaScript regular expressions is beneficial for any developer looking to enhance their coding skills. Among the more advanced and powerful features within this subject are lookahead and lookbehind assertions. These assertions enable developers to search for patterns in text without including those patterns in the result. This guide aims to provide an in-depth exploration of lookahead and lookbehind assertions in JavaScript, complete with numerous practical examples.

Warning

Regular expressions can be tricky, and edge cases may exist. Especially when you are using advanced concepts like Lookahead and Lookbehind. Use online regex testers or write unit tests to validate your expressions against a variety of input scenarios to ensure they behave as expected.

Introduction to Lookahead and Lookbehind Assertions

Lookahead and lookbehind assertions are zero-width assertions, meaning they match a position within the string rather than actual characters. They allow us to assert whether a given pattern exists or does not exist at a specific point in the string. Note: Modern JavaScript engines (ES2018+) support variable-length lookbehind assertions, though some older environments may require fixed-length patterns.

Positive Lookahead

A positive lookahead assertion checks for the existence of a certain pattern ahead of the current position in the string. It is denoted by (?=...). The following example is a positive lookahead to find the word "apple" followed by the word "pie":

Javascript regexp lookaround corresponds to characters

javascript— editable

Negative Lookahead

A negative lookahead assertion checks for the absence of a certain pattern ahead of the current position. It is denoted by (?!...). The following example is a negative lookahead to find the word "apple" not followed by the word "pie":

Javascript regexp the negative lookahead

javascript— editable

Positive Lookbehind

A positive lookbehind assertion checks for the existence of a certain pattern behind the current position in the string. It is denoted by (?<=...). Below is a code sample to find the word "pie" that is preceded by the word "apple":

Javascript regexp the positive lookbehind

javascript— editable

Negative Lookbehind

A negative lookbehind assertion checks for the absence of a certain pattern behind the current position. It is denoted by (?<!...). Here is a sample to find the word "pie" that is not preceded by the word "apple":

Lookahead and Lookbehind

javascript— editable

Practical Applications of Lookahead and Lookbehind

Validating Password Strength

Ensuring strong passwords is a common requirement in web applications. Lookahead assertions can be used to validate various password conditions without consuming characters.


javascript— editable

In this example we validated a password that must contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

Formatting and Parsing Data

Lookahead and lookbehind assertions can assist in formatting and parsing complex data structures. A good example is inserting commas into a string of numbers for readability:


javascript— editable

Conclusion

Mastering lookahead and lookbehind assertions in JavaScript can significantly enhance your ability to manipulate and analyze strings. These powerful tools provide the flexibility to assert patterns without consuming characters, making them invaluable for complex text processing tasks. By leveraging these assertions, developers can create more efficient and effective regular expressions, leading to cleaner and more maintainable code.

Practice

Practice

Which of the following statements about lookahead and lookbehind assertions in JavaScript are correct?