Regex Syntax

Regex Overview

Regex Syntax

🙋 Need help? Ask an expert now!

Regular expressions is an extremely powerful tool implemented in most programming languages. Regular expressions have their own syntax and the use of special characters that are often hard to remember unless they are used frequently. Regular expression can be tested online on sites such as or In JavaScript, regular expression are built-in and can be created using regex literals or a function constructor. Regex literals are useful for static expressions while function constructors are useful when the expression depends on other parameters.

The code sample above shows an example of a regex literal. The constant literalRegex is used to represent a regex expression that matches any string that ends with the letter 'y'. Therefore, the first console.log() statement prints the last index of the string while the second statement prints -1 (meaning it does not exist). The second example of a regex is shown using a function constructor. The function constructor uses the keyword new to create a regular expression. The new regex matches any string that starts with the letter 'T'. The two console.log() statements that follow simply check if this condition is met and returns the index of the string (or -1).

Edit Me on GitHub!

Application Question

Which of the following regex literals match strings that end with a 'T'?