Regex (short for Regular Expressions) is a powerful tool used to match and extract patterns from strings—commonly applied in tasks like validating input, filtering data, and parsing content. In this tutorial, we’ll walk through three beginner-friendly examples inside a real React/Next.js app to help you understand how regex works and how you can use it effectively in your own projects.
Why Regex?
Pattern matching in strings
Substring extraction
Validation
Replacement
Filtering
Example 1: Validation
What if you want to make sure users only enter English letters as their username? Let’s say you’re building a signup form and want to enforce a simple rule to only allow alphabet characters (a–z) with no numbers, no spaces, no symbols.
This is a perfect use case for Regular Expressions (Regex).
In this example, we use the regex pattern /^[a-z]+$/i to validate the input:
The pattern is wrapped inside forward slashes (/.../), this is JavaScript’s way of defining a regular expression.
Think of ^ and $ as boundary markers that wrap your validation logic. Without them, the pattern can match anywhere in the string — but with them, the entire string must follow the rules exactly.
[a-z] means allow one English letters. [a-z]+ means allow one or more English characters.
The i flag makes the match case-insensitive, so both uppercase and lowercase letters are accepted.
So the whole pattern matches strings made entirely of English letters (like John, Jane, or Awesome), and rejects anything else (like john_doe, 123Jane, or awesome!).
In JavaScript, regex is commonly used with the .test() method to check whether a string matches a specific pattern. The .test() method returns a boolean, true if the pattern is found, and false if it’s not.
Example 2: Category Filtering
In this example, we introduce RegExp(). It works like /.../ but allows you to build regular expressions dynamically using variables — perfect for cases like category filters or user input.
This example demonstrates how to filter items based on a selected category using RegExp() which is handy when you need dynamic matching logic. It also introduces two helpful JavaScript methods: .flatMap() and .filter().
Imagine you have a list of book categories, each with its own array of book titles. You want users to click on a category (e.g. Fiction or Crime), and instantly see only the books in that category.
To accomplish this:
We flatten all books from all categories using .flatMap().
We dynamically build a regex pattern with new RegExp() to match books that belong to the selected category.
We filter the list of all books using .filter() and the regex pattern.
const allBooks = AllBooks.flatMap(item => item.books); It goes through each item, accesses the books array (hence item.books), and flattens all of them into a single array. So when we map through allBooks, we can display all books.
We use RegExp() instead of /.../ so we can insert the selectedCategory variable into the pattern dynamically.
allBooks.filter(book => pattern.test(book)); loops through each item inside allBooks variable and returns only the ones that match the pattern effectively filtering the array based on the selected category.
Example 3: Extract Hashtags from Input
In this example, we use regex to extract hashtags in real time as the user types. The extracted hashtags are displayed as pill-style tags below the input field.