regex — test a regular expression against a text
SYNOPSIS
regex <pattern> [flags] <text...> regex /<pattern>/<flags> <text...> regex <pattern> [flags] (read the text from piped input)
DESCRIPTION
Runs a JavaScript regular expression on a text and shows what it finds: the text with every match highlighted, then each match with its position (start-end, counted from 0) and the content of its capture groups, numbered ($1, $2…) and named (<name>).
Every match is listed, whether or not the g flag is given. An invalid pattern is reported with the engine's own error message.
Quote the pattern and the text so the shell keeps them in one piece.
OPTIONS
flags any of the JavaScript flags, written right after the pattern: i ignore case m ^ and $ match at line breaks s the dot matches \n u Unicode mode (\p{…}, code points) y sticky g accepted (matching is always global) -- put it after the pattern when the text starts with a word made of flag letters only: regex a -- is this a test
A word is read as flags when it only contains flag letters and some text follows it (or text is piped in). The /pattern/flags form never guesses.
HOW IT WORKS
The pattern is compiled by your browser's regular expression engine, the same one JavaScript code uses (ECMAScript syntax: lookahead and lookbehind, named groups, Unicode properties). Results may differ slightly from PCRE (PHP, grep -P), Python or Go.
The search runs in a background thread with a two-second budget. Some patterns, such as (a+)+$ on a long run of "a", make a backtracking engine try an exponential number of paths — "catastrophic backtracking". Here the thread is simply stopped and the command says so, instead of freezing the page. Ctrl+C stops it too. Nothing is sent anywhere.
USE CASES
- check that a pattern captures what you expect before using it in code;
- understand someone else's regular expression on a real sample;
- try a validation rule (e-mail, date, identifier) on good and bad inputs;
- find out whether a slow pattern is prone to catastrophic backtracking.
EXAMPLES
regex "\d+" "order 66 shipped in 3 days" regex "(\w+)@(\w+)\.com" i "Mail Bob@Example.com or amy@test.com" regex "/(?<key>\w+)=(?<value>\w+)/g" "a=1, b=2" cat about.md | regex "https?://\S+"