Understanding Regex - The Cheatsheet

Your go-to resource for Effective text processing. Learn syntax, patterns and practical examples to boost yout skills and productivity

Ramiro M. avatarRamiro M.
Understanding Regex - The Cheatsheet

helloimnik | Unsplash

What are Regular Expressions?

Regular expressions, commonly referred to as "regex" or "regexp", are powerful patterns used by programmers to match, search, and manipulate text. They provide a flexible and concise way to specify complex patterns that can be used in various programming languages and text editors.

Regular expressions are composed of literal characters and special characters, also known as metacharacters, which have predefined meanings within the pattern. By combining these characters, you can create patterns that match specific sequences of text.

The power of regular expressions lies in their flexibility. They allow you to specify complex patterns with just a few characters, saving you time and effort. Once you've created a regular expression pattern, you can use it in various programming languages, text editors, and command-line tools.

Greedy Match

Imagine you have a regular expression pattern that you want to apply to a string and extract a specific part of it. In some cases, you may want to find the longest possible match that satisfies the pattern. This is where greedy matching comes into play.

Greedy matching is the default behavior of regular expressions. It means that the regex engine will try to match as many characters as possible while still satisfying the pattern. It's like a hungry monster that wants to consume as much text as it can.

Let's take an example to understand it better. Suppose we have the regex pattern a.*b and the string aabab. The pattern consists of the literal character a, followed by the metacharacter .*, which means any number of characters (including zero), and finally the literal character b. In this case, the greedy match will consume the entire string aabab because the .* allows for any number of characters.

While greedy matching can be helpful in many situations, there are times when you might want to use a different approach. For instance, when dealing with large texts, greediness can lead to performance issues as it exhaustively searches for the longest match. Additionally, if you want to perform more specific matches, greediness may not give you the desired result.

Lazy Match

In contrast to the greedy match, the lazy match, also known as non-greedy or minimal match, finds the smallest possible part of the string that satisfies the regex pattern. It matches as few characters as possible while still meeting the pattern's requirements.

To perform a lazy match, you can add a question mark (?) after a quantifier. The question mark modifies the quantifier to become lazy. For example, the regex pattern a.*?b applied to the string 'aabab' will match the substring aab because the .*? only matches the minimum number of characters necessary to satisfy the pattern.

Lazy matching is particularly useful when you want to match specific parts of a string without consuming unnecessary characters. It can prevent excessive matching and improve the efficiency of your regular expressions.

Understanding both greedy and lazy matching allows you to choose the appropriate approach based on your specific needs when working with regular expressions.

JavaScript Methods

When you have a regular expression pattern in JavaScript, you can test it against a string using the .test() method. This method returns true if the pattern matches any part of the string and false otherwise. It's a simple way to check if a pattern exists in a string without extracting any specific information.

If you need to extract specific parts of the string that match the pattern, you can use the .match() method. This method returns an array containing the matches found or null if no match is found. You can also use capturing groups within your regular expression pattern to extract specific portions of the matched string.

In more complex scenarios, you can use the .exec() method to obtain detailed information about the matches. This method returns an array containing the matched string, as well as additional properties like the index of the match and the groups captured by the pattern.

Apart from testing and extracting, JavaScript also provides methods like .replace() and .split() for manipulating strings using regular expressions. The .replace() method allows you to replace matched portions of the string with new content, while the .split() method splits the string into an array based on the matches found.

Regular expressions in JavaScript also support flags that modify the behavior of the pattern matching. For example, the g flag enables global matching, searching for all occurrences of the pattern rather than stopping at the first match.

By leveraging these regular expression methods in JavaScript, you can perform powerful string operations with ease. Whether you need to validate input, extract specific information, or transform strings based on patterns, regular expressions in JavaScript are an invaluable tool.

So, if you're working with strings and need to perform pattern matching or manipulation, don't forget to harness the power of regular expressions in JavaScript. They'll help you achieve efficient and precise text processing, saving you time and effort along the way.


  • The .test() method is a JavaScript method that is used to test whether a given regular expression pattern matches a string. It takes a regular expression (regex) as an argument and applies it to a specified string. The regex is enclosed within forward slashes (/regex/).

In the example provided:

let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Returns true
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // Returns true

The myRegex.test(myString) statement checks if the regex pattern /Hello/ matches the string "Hello, World!". In this case, since the string contains the word "Hello", the method returns true, indicating that the pattern was found in the string.

The .test() method returns a boolean value (true or false) based on whether the pattern is found or not. If the pattern is found, it returns true; otherwise, it returns false. It does not provide the actual matches found within the string.


  • The .match() method is another JavaScript method used to search for matches between a regular expression and a string. It allows you to extract the actual matches found within the string.

In the example provided:

let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/;
let result = extractStr.match(codingRegex); // Returns ["coding"]
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/;
let result = extractStr.match(codingRegex); // Returns ["coding"]

The extractStr.match(codingRegex) statement applies the codingRegex pattern (/coding/) to the extractStr string. It searches for the occurrence of the word "coding" within the string and returns an array ["coding"], which contains the matching substring.

The .match() method returns an array containing all the matches found in the string. If no matches are found, it returns null. In this case, since the word "coding" is present in the extractStr, the method returns an array with a single element, which is the matching substring "coding".

Patterns and flags

Alternation or OR operatorOperator|/yes | no | maybe/This operator matches patterns either before or after it. You can also search for more than just two patterns.
Wildcard PeriodOperator./hu./The wildcard character . will match any one character. The wildcard is also called dot and period. For example, if you wanted to match hug, huh, hut, and hum, you can use the regex /hu./ to match all four words.
Character classesOperator[ ]/b[aiu]g/You can search for a literal pattern with some flexibility with character classes. Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets.
Character setsOperator-/a-e/ /1-3/g /[a-z0-9]/igInside a character set, you can define a range of characters to match using a hyphen character: -
Negated Character setsOperator^/[^aeiou]/giTo create a negated character set, you place a caret character (^) after the opening bracket and before the characters you do not want to match.
Caret characterOperator^/^Hello/Outside of a character set, the caret is used to search for patterns at the beginning of strings.
Dollar signOperator$/story$/You can search the end of strings using the dollar sign character $ at the end of the regex.
One or more timesOperator+/a+/gTo match a character (or group of characters) that appears one or more times in a row. This means it occurs at least once, and may be repeated. The character or pattern has to be present consecutively.
Zero or more timesOperator\*/go\*/Matches anything in the place of the *, or a "greedy" match
Lazy matchOperator?/t[a-z]\*?i/Finds the smallest possible part of the string that satisfies the regex pattern.
See all posts