Regex (short for regular expression) is a sequence of characters that defines a search pattern. Regex is used to match, find, replace or validate strings — from simple word searches to complex text processing tasks. Every major programming language includes a built-in regex engine, making it one of the most universally applicable tools in a developer's toolkit.
Want to test a regular expression interactively?
Open Regex Tester →Regular expressions originate from formal language theory. Mathematician Stephen Cole Kleene introduced them in the 1950s as a notation for describing regular languages. Ken Thompson implemented regex in the UNIX text editor ed and later in grep (1973), making them widely available to developers. Perl (1987) expanded the syntax significantly and popularized the PCRE (Perl-Compatible Regular Expressions) dialect that most modern languages follow today.
A regular expression is a pattern written between delimiters (in JavaScript: /pattern/flags). The pattern can contain literal characters, special metacharacters and quantifiers. The regex engine scans a string and reports every position where the pattern matches.
/* Email validation regex */
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
/* Match: alice@example.com */
/* No match: notanemail */
cat matches the string "cat"[aeiou] matches any vowel, [a-z] any lowercase letter, [^0-9] any non-digit^ matches the start of a string, $ matches the end* (0 or more), + (1 or more), ? (0 or 1), {n} (exactly n), {n,m} (between n and m). matches any single character except a newlinecat|dog matches either "cat" or "dog"(abc) capturing group, (?:abc) non-capturing group\ before a metacharacter treats it as a literal: \. matches a period\d (digit), \w (word character), \s (whitespace), and their negations \D, \W, \S^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$https?://[^\s]+\d{4}-\d{2}-\d{2}\+?[\d\s\-\(\)]{7,15}\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b#[0-9a-fA-F]{3,6}\d{5}(-\d{4})?Flags modify how the regex engine interprets the pattern:
g — global — find all matches, not just the firsti — case-insensitive — /hello/i matches "Hello", "HELLO", "hello"m — multiline — ^ and $ match the start and end of each lines — dotAll — . also matches newline charactersu — unicode — enables full Unicode matching. * + ? ^ $ { } [ ] | ( ) \ must be escaped with \ to be treated as literals*, +) match as much as possible; add ? after them (*?, +?) for lazy matching^ anchor means "start of string" outside a character class but "negation" inside one: [^abc]Regex (short for regular expression) is a sequence of characters that defines a search pattern. It is used to match, find, replace or validate strings in text. Every major programming language supports regular expressions.
Regex stands for regular expression. The term comes from formal language theory developed by mathematician Stephen Cole Kleene in the 1950s.
Regex is used for input validation (email, phone, password), search and replace in text editors, log file parsing, URL routing in web frameworks, and data extraction from unstructured text.
Most languages support a common core syntax based on PCRE (Perl-Compatible Regular Expressions), but there are differences in flags, some special characters and advanced features like lookbehind. JavaScript, Python, Java and PHP all use PCRE-compatible engines.
The * quantifier matches zero or more occurrences of the preceding element. The + quantifier matches one or more occurrences. So a* matches "", "a", "aa", ... while a+ matches "a", "aa", ... but not an empty string.
The ^ character has two meanings. At the start of a pattern it is an anchor that matches the beginning of the string (or line in multiline mode). Inside a character class [^abc] it means negation — match any character except those listed.