Home >Blog

How to Test Regex in JavaScript: test(), match(), exec(), and matchAll()

Published Updated

Four Ways to Test a Regex in JavaScript

JavaScript gives you four common methods to run a regular expression against a string: test(), match(), exec(), and matchAll(). They look similar but return very different things, and picking the wrong one is the source of many subtle bugs.

test(): The Fastest Yes/No Check

regex.test(string) returns a boolean. Use it when you only care whether something matches, such as form validation.

const emailOk = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(input);
// true or false

match(): Get the Matched Substring

string.match(regex) returns the first match (with capture groups) as an array, or null. Without the g flag it also gives you index, input, and named groups. With the g flag it returns an array of all matched substrings and loses group details.

"order #1024".match(/#(\d+)/);
// ["#1024", "1024"]

"a1 b2 c3".match(/[a-z]\d/g);
// ["a1", "b2", "c3"]

exec(): The Stateful Workhorse

regex.exec(string) returns one match at a time and advances the regex's lastIndex property. Call it in a loop to walk through all matches while keeping capture groups and indexes.

matchAll(): Iterate All Matches Safely

string.matchAll(regex) requires the g flag and returns an iterator of match objects, each with groups and index. It is the modern, loop-friendly replacement for manual exec() walks.

const re = /([a-z])(\d)/g;
for (const m of "a1 b2".matchAll(re)) {
  console.log(m[1], m[2], m.index);
}

The lastIndex Trap With the g Flag

This is the part that bites people. When a regex has the g (or y) flag, test() and exec() are stateful: they remember lastIndex from the previous call and start searching there next time. Reusing the same regex object across calls causes alternating true/false results.

const re = /\d+/g;
re.test("12 34"); // true, lastIndex now 2
re.test("12 34"); // false, starts at index 2!

// Fix: create a fresh regex each time, reset re.lastIndex = 0,
// or use String.prototype.match / matchAll for stateless checks.

Pick the Right Tool

Use test() for validation booleans, match() for one-off substrings, matchAll() when you need all matches with groups, and exec() only when you explicitly want to drive iteration yourself. Always be aware of the g flag and lastIndex.

Test Regex Live in Your Browser

Want to experiment without redeploying? Paste your pattern and sample text into the free online regex tester on Regex Toolbox. It highlights matches in real time so you can see exactly where lastIndex and the g flag change the result.

目次

情報

  • ヒット115
  • 公開日2026/09/21
0/500
敬意をもってご意見をお聞かせください。

もっと投稿

このセクションの記事をさらに探索
Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples
Sep 09, 2026288ビュー

Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples

Use regex to find possible IP addresses, then use the right validation rule for the job. This guide covers loo...

#regex for IP address#IP address regex#IPv4 regex#IPv6 regex
Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords
Sep 07, 2026268ビュー

Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords

Learn practical password validation regex patterns for length, digits, symbols, and mixed character classes. T...

#password validation regex#regex for password validation#strong password regex#regex lookahead
What Regex Should You Use for Phone Numbers? Patterns for US and International Formats
Aug 28, 2026256ビュー

What Regex Should You Use for Phone Numbers? Patterns for US and International Formats

Compare practical regex patterns for US formatted numbers, normalized local numbers, plus-prefixed internation...

#phone number regex#regex for phone number#US phone regex#international phone regex
How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings
Sep 03, 2026245ビュー

How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings

Build a practical HTTP and HTTPS URL regex by separating schemes, DNS domains, ports, paths, query strings, an...

#URL validation regex#regex for URL validation#HTTP URL regex#HTTPS URL regex
How to Extract Email Addresses from Text with Regex in Python and JavaScript
Sep 15, 2026211ビュー

How to Extract Email Addresses from Text with Regex in Python and JavaScript

Extract email addresses from any text with regex in Python and JavaScript: the pattern explained piece by piec...

#regex extract email#email regex python#email regex javascript#extract emails from text
Regex for Numbers Only: Integers, Decimals, Negative Values, and Leading Zeros
Sep 12, 2026211ビュー

Regex for Numbers Only: Integers, Decimals, Negative Values, and Leading Zeros

Choose the right regex for numbers-only input. This guide covers ASCII digits, integers, decimals, negative va...

#regex for numbers only#numbers only regex#integer regex#decimal regex
フィードバックメール