Home·Regular expressions
Ad

Claim your crypto exchange bonus

Bonuses you get just for signing up. Terms are copied straight from each exchange.

133 regular expressions

From the notation itself to checks for email, dates and addresses. Every expression comes with examples that should match and examples that should not — and the test suite actually runs them.

Notation36

How to point at a single character. Everything else builds on these.

Repeats and groups24

How many times something repeats, and what counts as one unit.

Looking around14

Checks what sits before or after without consuming it. Useful for stacking conditions.

Whole-string checks66

Anchored at both ends, so the whole string has to match. This is what input validation uses.

^[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+$an email address^https?://[^\s/$.?#][^\s]*$a web address^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$a domain name^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$an IPv4 address^(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$an IPv6 address written out in full^(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2}$a MAC address^(?:6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4})$a port number^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$a UUID^#(?:[0-9a-f]{3}|[0-9a-f]{6})$a hex colour code^rgb\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\)$an rgb() colour value^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$a date in ISO form^(?:0[1-9]|[12]\d|3[01])/(?:0[1-9]|1[0-2])/\d{4}$a date written with slashes^(?:[01]\d|2[0-3]):[0-5]\d$a time on the 24-hour clock^(?:0?[1-9]|1[0-2]):[0-5]\d\s?(?:am|pm)$a time with am or pm^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$a full ISO timestamp^P(?=\d|T\d)(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+S)?)?$an ISO duration^\d+\.\d+\.\d+(?:-[0-9a-z.-]+)?(?:\+[0-9a-z.-]+)?$a semantic version number^\d+$a whole number with no sign^[+-]?\d+$a whole number that may carry a sign^-?\d+(?:\.\d+)?$a number that may have a decimal part^-?\d+(?:\.\d+)?[eE][+-]?\d+$a number in scientific notation^(?:100(?:\.0+)?|\d{1,2}(?:\.\d+)?)%$a percentage from 0 to 100^\d{1,3}(?:,\d{3})*$a number grouped with commas^[$€£¥₩]\s?\d{1,3}(?:,\d{3})*(?:\.\d{2})?$an amount with a currency sign^[0-9a-f]+$a hexadecimal string^[01]+$a string of nothing but 0 and 1^[0-7]+$an octal string^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$a base64-encoded string^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$a JWT split by two dots^\+[1-9]\d{7,14}$a phone number in international form^[a-z][a-z0-9_]{2,15}$a username that starts with a letter^[a-z0-9]+(?:-[a-z0-9]+)*$a URL slug^[^\\/:*?"<>|]+\.[a-z0-9]+$a file name with an extension^.+\.(?:jpe?g|png|gif|webp|avif|svg)$an image file extension^/(?:[^/\0]+/)*[^/\0]*$a Unix-style path^[A-Za-z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$a Windows path^</?[a-z][a-z0-9]*(?:\s[^<>]*)?/?>$a single HTML tag^\.-?[_a-z][_a-z0-9-]*$a CSS class selector^M{0,3}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})$a Roman numeral^-?(?:90(?:\.0+)?|[1-8]?\d(?:\.\d+)?)$a latitude value^-?(?:180(?:\.0+)?|1[0-7]\d(?:\.\d+)?|\d{1,2}(?:\.\d+)?)$a longitude value^(?:0|[1-9]\d*)$a number with no leading zero^\d*[02468]$an even number^\s*$a blank line^\d{4}-W(?:0[1-9]|[1-4]\d|5[0-3])$an ISO week number, up to week 53^#(?:[0-9a-f]{4}|[0-9a-f]{8})$a hex colour that carries alpha^\d{4}(?:[ -]?\d{4}){3}$a card number written in four blocks of four^[A-Z]{3}$a three-letter currency code^0x[0-9a-f]+$a hex number written with the 0x prefix^[0-9a-f]{64}$a sixty-four character hash^[0-9a-f]{24}$a twenty-four character document id^\d{5}$a five-digit postal code^[A-Z]{2}$a two-letter country code^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$an IPv4 address with a CIDR prefix^01[016789]-?\d{3,4}-?\d{4}$a Korean mobile number^[가-힣]{2,5}$a Hangul name of two to five syllables^\d{5}(-\d{4})?$a US ZIP code^([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})*)?::([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})*)?$a compressed IPv6 address^\d{3}-\d{2}-\d{5}$a Korean business registration number^[a-z]+\/[a-zA-Z0-9][a-zA-Z0-9.+-]*$a MIME type^data:[a-z]+\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/]+=*$a base64 data: URI^(\S+\s+){4}\S+$a five-field cron expression^[A-Za-z]:\\.*$a Windows drive path^[\^~]?\d+\.\d+\.\d+$a semver range with ^ or ~^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3})$a private IPv4 range^[A-Z][a-zA-Z0-9]*$a PascalCase name

Finding and cleaning49

For picking out or deleting just the part you need from a larger text.

\s+$space left at the end of a line^\s+space at the start of a line^\s+|\s+$space at either end\s{2,}two or more spaces in a row\n\s*\nan empty line between two lines\D+everything that is not a digit[^\x00-\x7F]a character outside ASCII[\x00-\x1F]an invisible control character</?[^>]+>the HTML tags in a text<!--[\s\S]*?-->an HTML comment//.*$a comment that starts with two slashes/\*[\s\S]*?\*/a comment that spans lines"[^"\\]*(?:\\.[^"\\]*)*"text inside double quotes'[^'\\]*(?:\\.[^'\\]*)*'text inside single quotes\[([^\]]+)\]\(([^)]+)\)a Markdown link^#{1,6}\s+.+$a Markdown heading line(?:^|\s)#([a-z0-9_]+)a hashtag(?:^|\s)@([a-z0-9_]{2,})an @ mention[?&]([^=&]+)=([^&]*)a query parameter in a URLhttps?://[^\s<>"]+a URL sitting inside ordinary text^https?://([^/:?#]+)just the domain out of a URL\.([a-z0-9]+)$the extension of a file name(?<=[a-z0-9])(?=[A-Z])the seam between words in camelCase_([a-z])the letter that follows an underscore-([a-z])the letter that follows a hyphen(?:^|,)("(?:[^"]|"")*"|[^,]+)one comma-separated field^([^=]+)=(.*)$a name and value split by an equals sign^\s*(\S+)the first word(\S+)\s*$the last word\d+runs of digits inside a text\b\w+\beach word, one by one\[([^\]]*)\]what sits inside square brackets\(([^)]*)\)what sits inside round brackets[\uD800-\uDBFF][\uDC00-\uDFFF]an emoji that takes two code units(.)\1{2,}the same character three times or more,\s*([}\]])a comma left before a closing bracket\*\*([^*]+)\*\*bold text wrapped in double asterisks`([^`]+)`code wrapped in backticks(\w+)="([^"]*)"an attribute whose value is in double quotes\x1b\[[0-9;]*ma terminal colour escape codefrom ['\"]([^'\"]+)['\"]the path in an import statement#[0-9a-fA-F]{6}\ba hex colour inside running textTODO:?\s*(.*)a TODO note left in a comment\d+(px|em|rem|%)a number with a CSS unit!\[([^\]]*)\]\(([^)]+)\)a Markdown image^[-*+]\s+(.+)$a Markdown list item--[^\n]*a SQL line comment\$\{?([A-Z_][A-Z0-9_]*)\}?a shell environment variable[A-Za-z0-9_-]{11}a YouTube video id

How to read this

  • With ^ and $ around it, the whole string must match; without them the match can sit anywhere. Always anchor an expression you use for validation.
  • Round brackets ( ) capture what they wrap. When you only need grouping, write (?: ) so the numbering of later groups does not shift.
  • Every expression here is checked against its examples. Even so, "looks like an email" and "is a real inbox" remain different questions.
  • Nested open-ended repeats such as (a+)+ can blow up on certain inputs. Look for that shape before pasting someone else’s expression into your code.

Frequently asked questions

QWhat is a regular expression?

A small language for describing a shape to look for in text. Writing \d{4} instead of "four digits" lets a program find every place that shape occurs. JavaScript, Python, Java and most other languages share nearly the same notation.

QWhy is the email check so short?

A pattern covering the full specification runs to thousands of characters and still cannot tell you whether the mailbox exists. Catching typos is what a regular expression can do; the only real check is sending a confirmation message.

QWhy do patterns differ between languages?

The core is shared but the details are not. JavaScript has no \A, and Python needs re.MULTILINE switched on separately. Everything here is written to run as-is in JavaScript.

QWhat does it mean that a pattern can be slow?

When open-ended repeats nest, as in (a+)+, the number of ways to backtrack explodes on input that does not match. A short string can then take seconds, which is dangerous on a server.

QCan I trust the patterns here?

Each one is stored with examples that must match and examples that must not, and the test suite runs all 133 for real. The examples you see on the page are the same ones the tests use, so a description cannot drift away from its pattern.