Home·Regular expressions

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.

Notation28

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

Repeats and groups17

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

Looking around8

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

Whole-string checks44

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

Finding and cleaning36

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

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

Q. What 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.

Q. Why 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.

Q. Why 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.

Q. What 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.

Q. Can 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.