JavaScript Regex Testing Example

Sarah Jul 13, 2026

JavaScript's RegExp object is a vital tool for working with patterns in strings, enabling complex text manipulation, data validation, and extraction. Test methods like test() and exec() facilitate this interaction. Let's delve into these methods with practical examples.

Working With Regular Expressions (RegEx) in JavaScript
Working With Regular Expressions (RegEx) in JavaScript

RegExp test() and exec() methods play a significant role in pattern matching. Here's how to use them effectively:

the text reads regex 1011 test and build regular expressions regex for free in an online editor, with real - time results
the text reads regex 1011 test and build regular expressions regex for free in an online editor, with real - time results

Using test() for pattern matching

test() is a quick way to check if a patterns exists in a string. It returns a Boolean: true if found, false otherwise. Here's a simple example:

JavaScript Recursion Simplified
JavaScript Recursion Simplified

const str = "Hello, World!"; const pattern = /world/i; console.log(pattern.test(str)); // Outputs: true

test() with flags

a computer screen with the word regex on it
a computer screen with the word regex on it

Test method accepts various flags (like i, g) to customize search behavior. The i flag ignores case, and g finds all matches instead of stopping at the first.

The code snippet below finds all comma-separated words:

/foo|bar/i.test('foo, bar, baz"); // Returns true, as 'foo' and 'bar' exist in the string.

Javascript Fundamental
Javascript Fundamental

Using exec() for pattern-finding and extraction

exec() returns an array of matches when found, and if no match exists. Unlike test, exec also captures groups and keeps track of the last index found.

Here's an example of using exec() with a group and capturing custom data:

JavaScript Regular Expressions (Regex) Cheat Sheet
JavaScript Regular Expressions (Regex) Cheat Sheet

const str = "My phone number is 123-456-7890"; const pattern = /phone(\d+)/i; // captures digits after 'phone' console.log(pattern.exec(str)); // Outputs: [ 'phone123-456-7890', '123-456-7890' ].

exec() with all matches using the global flag

a computer screen with some type of programming program on it's back side and the words,
a computer screen with some type of programming program on it's back side and the words,
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript calculator projet
JavaScript calculator projet
Javascript Cheet cheet
Javascript Cheet cheet
JavaScript Quiz: Understanding Array and Object References #1
JavaScript Quiz: Understanding Array and Object References #1
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Reserved Words: What You Need to Know 🚀
JavaScript Reserved Words: What You Need to Know 🚀
JavaScript Fundamental
JavaScript Fundamental
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
Regex Tester: Validate and Test Your Patterns
Regex Tester: Validate and Test Your Patterns
an info sheet with different types of web pages
an info sheet with different types of web pages
5 Ways To Add Elements To Javascript Array
5 Ways To Add Elements To Javascript Array
JavaScript Array reduce() Method
JavaScript Array reduce() Method
JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
JavaScript Increment & Decrement Operators: Simplify Your Code 🔄
Beginner's Guide to Regular Expression (Regex)
Beginner's Guide to Regular Expression (Regex)
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
Job Ready Progress Checklist for JavaScript
Job Ready Progress Checklist for JavaScript
Javascript Loops & Iterations
Javascript Loops & Iterations
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀
JavaScript Arrow Functions: Write Shorter & Cleaner Code 🚀

Using g flag, exec() can find all matches in a string and return an array of all captured values. Each call to exec() returns the next match:

/foo/g.exec('foo, this, and foo too'); // Returns: [ 'foo' ]

In practice, the exec() method is helpful for string manipulation and data extraction, especially when you need to handle multiple matches and capture groups. Test these methods in your projects to harness the full potential of JavaScript's RegExp power.