Skip to main content

Regex Tester

Test and debug regular expressions with live matching and group capture

Advertisement

Advertisement
//g
Advertisement
On this page

About Regex Tester

What is Regex

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Used in text processing, regex allows you to find, validate, replace, and extract specific parts of strings with precision. Regular expressions are supported in virtually every programming language, text editor, and command-line tool, making them one of the most universally useful skills in software development.

The concept of regular expressions originated in theoretical computer science in the 1950s, when mathematician Stephen Kleene formalized the idea of regular sets and regular events. Since then, regex has evolved into a practical tool embedded in tools like grep, sed, awk, Perl, and modern programming languages. While the core syntax is standardized, each language adds its own extensions and features.

JavaScript implements regex through the RegExp object and regex literals like /pattern/flags. This tool uses the browser's native RegExp engine, so all patterns you test here behave exactly as they would in your JavaScript code.

Common Use Cases

Form validation: Regex is the standard way to validate user input such as email addresses, phone numbers, postal codes, and passwords. HTML5 form validation even supports regex directly through the pattern attribute on input elements.

Search and replace: Text editors and IDEs use regex for powerful find-and-replace operations. You can search for complex patterns across entire codebases and transform matched text using capture groups and backreferences.

Data extraction: Regex is commonly used to parse logs, extract information from HTML or XML, scrape data from websites, and process CSV or delimited files. Capture groups let you pull out specific parts of each match.

URL routing: Web frameworks use regex to map URL patterns to handler functions, extracting parameters from the URL path in the process. For example, matching /users/:id and extracting the ID value.

Text transformation: Regex combined with replacement functions can transform text in sophisticated ways, such as converting camelCase to snake_case, formatting numbers with thousand separators, or sanitizing user input by removing unwanted characters.

JavaScript Regex Syntax

Literals and constructors: A regex can be created as a literal /abc/ or via the constructor new RegExp("abc"). Literals are compiled at parse time, while constructors allow dynamic patterns.

Character classes: Use square brackets to match any one of a set of characters. [a-z] matches any lowercase letter, [0-9] matches any digit, and [^a-z] matches anything except lowercase letters. Shorthand classes like \d (digit), \w (word character), and \s (whitespace) are also available.

Quantifiers: Specify how many times a pattern should match. * means zero or more, + means one or more, ? means zero or one, and {n,m} means between n and m times. Use lazy quantifiers like *? and +? to match as few characters as possible.

Flags: Flags modify how the regex engine interprets the pattern. g finds all matches (global), i makes the match case-insensitive, m makes ^ and $ match line boundaries in multiline strings, and s allows the dot to match newline characters (dotAll mode).

Capture groups: Parentheses () create capture groups that remember the matched text for later reference. Non-capturing groups (?:) group without capturing. Named groups (?<name>...) allow accessing groups by name instead of index.

Anchors: ^ asserts position at the start of the string (or line in multiline mode), and $ asserts position at the end. \b matches a word boundary, useful for matching whole words only.

FAQ

What regex engine does this tool use?

This tool uses the browser's built-in JavaScript RegExp engine, which implements the ECMAScript regex specification. Patterns you test here will behave identically in your JavaScript or TypeScript code.

Does this tool send my data to a server?

No. All regex matching is performed entirely in your browser. Neither your pattern nor your test string is transmitted to any external server, making it safe to test with sensitive data.

Why does my regex with the "g" flag sometimes cause an infinite loop?

A regex with the global flag can enter an infinite loop if it matches an empty string and does not advance. For example, /(?:)/g matches at every position. This tool handles this by advancing the last index after a zero-length match to prevent the browser from hanging.

Can I use named capture groups?

Yes. Modern browsers support named capture groups with the (?<name>...)syntax. However, this tool's capture group table currently displays groups by index. Named groups are still correctly matched and can be accessed in your JavaScript code via match.groups.name.

What is the difference between the "m" and "s" flags?

The m (multiline) flag changes the behavior of ^ and $ so they match at the start and end of each line rather than only the start and end of the entire string. The s (dotAll) flag makes the dot . match newline characters, which it normally does not. These flags are independent and can be used together.

Advertisement

Advertisement

This tool is provided for informational purposes only. KnowKit is not responsible for any errors in the output.

You might also like

Advertisement