Regex Samples

Common JavaScript regular expression patterns with plain-language explanations and one-click testing.

A starting point for patterns that come up often in forms, logs, and text processing. Each example uses JavaScript regex syntax and can be opened in the live tester.

Use Pattern What it matches Action
Digits only ^\d+$ One or more digits, with nothing else before or after. Use this for a whole-number string, not decimals or negative numbers. Test
Username ^[A-Za-z0-9_]{3,16}$ Three to sixteen ASCII letters, digits, or underscores. Adjust the length range and allowed characters to match your rules. Test
Email-shaped text ^[\w.+-]+@[\w-]+(?:\.[\w-]+)+$ A practical check for a typical email shape. Email address rules are more complex, so use confirmation email rather than this pattern as proof an address is valid. Test
HTTP or HTTPS URL ^https?:\/\/[\w.-]+(?::\d+)?(?:[/?#][^\s]*)?$ A basic web URL with an optional port and path, query, or fragment. It is intentionally not a full URL parser and does not cover every valid hostname. Test
Date (YYYY-MM-DD) ^\d{4}-\d{2}-\d{2}$ Checks the four-digit year, two-digit month, and two-digit day shape only; it will also match impossible dates such as 2026-19-42. Test
UUID ^[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$ Matches the standard 8-4-4-4-12 hexadecimal UUID layout. It checks the shape, not a particular UUID version. Test
Hex colour ^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ Matches a 3- or 6-digit hexadecimal colour, with an optional leading #. Test
URL slug ^[a-z0-9]+(?:-[a-z0-9]+)*$ Lowercase letters and digits separated by single hyphens, with no leading, trailing, or repeated hyphens. Test
Repeated whitespace \s{2,} Finds runs of two or more whitespace characters, including spaces, tabs, and line breaks. Test
Hashtag #[A-Za-z0-9_]+ Finds a # followed by one or more ASCII letters, digits, or underscores. Test

Patterns are examples, not complete validation rules. For dates, email addresses, URLs, and other structured values, use a dedicated parser or server-side validation when correctness matters.

Open the Regex Tester to try a pattern against your own text.