Skip to main content
CheckTown

正则表达式库

按类别浏览常用的正则表达式模式

在我们的正则表达式测试器中测试你的模式

电子邮件

Simple Email

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Matches a basic email address format

匹配: user@example.com不匹配: user@.com

Email with Display Name

^[^<]*<[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}>$

Matches an email with a display name like "John Doe <john@example.com>"

匹配: John Doe <john@example.com>不匹配: john@example.com

Email Domain Extraction

@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})

Extracts the domain part from an email address

匹配: user@example.com不匹配: no-at-sign-here

No-Reply Email

^no-?reply@

Matches email addresses starting with no-reply or noreply

匹配: no-reply@example.com不匹配: contact@example.com

URL

URL with Protocol

^https?:\/\/[^\s/$.?#].[^\s]*$

Matches a URL starting with http:// or https://

匹配: https://example.com/path不匹配: ftp://example.com

URL Slug

^[a-z0-9]+(?:-[a-z0-9]+)*$

Matches a URL-safe slug with lowercase letters, numbers, and hyphens

匹配: my-blog-post不匹配: My Blog Post

Domain Name

^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$

Matches a valid domain name like example.com or sub.domain.org

匹配: sub.example.com不匹配: -invalid.com

URL Query Parameter

[?&]([^=]+)=([^&]*)

Extracts key=value pairs from URL query strings

匹配: ?name=John&age=30不匹配: /path/without/params

YouTube Video ID

(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})

Extracts the 11-character video ID from YouTube URLs

匹配: https://youtu.be/dQw4w9WgXcQ不匹配: https://vimeo.com/12345

日期与时间

ISO 8601 Date

^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$

Matches dates in ISO 8601 format (YYYY-MM-DD)

匹配: 2024-12-31不匹配: 31-12-2024

Date (MM/DD/YYYY)

^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$

Matches dates in US format (MM/DD/YYYY)

匹配: 12/31/2024不匹配: 31/12/2024

Date (DD/MM/YYYY)

^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$

Matches dates in European format (DD/MM/YYYY)

匹配: 31/12/2024不匹配: 2024-12-31

24-Hour Time

^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$

Matches time in 24-hour format with optional seconds

匹配: 23:59:59不匹配: 25:00

12-Hour Time

^(0?[1-9]|1[0-2]):[0-5]\d\s?(?:AM|PM|am|pm)$

Matches time in 12-hour format with AM/PM

匹配: 2:30 PM不匹配: 13:30 PM

数字

Integer

^-?\d+$

Matches positive or negative whole numbers

匹配: -42不匹配: 3.14

Decimal Number

^-?\d+\.?\d*$

Matches positive or negative numbers with optional decimal part

匹配: 3.14不匹配: abc

Hex Color

^#(?:[0-9a-fA-F]{3}){1,2}$

Matches 3 or 6 digit hex color codes with # prefix

匹配: #ff6600不匹配: #gggggg

Currency (USD)

^\$\d{1,3}(,\d{3})*(\.\d{2})?$

Matches US dollar amounts with optional cents and thousands separators

匹配: $1,234.56不匹配: 1234.56

Percentage

^-?\d+(\.\d+)?%$

Matches percentage values with optional decimal part

匹配: 99.9%不匹配: 99.9

电话

International Phone

^\+?[1-9]\d{1,14}$

Matches international phone numbers in E.164 format

匹配: +14155552671不匹配: 0000000

US Phone

^(\+1)?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

Matches US phone numbers in various formats

匹配: (415) 555-2671不匹配: 123

Phone with Extension

^(\+?\d{1,3})?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?$

Matches phone numbers with optional country code and extension

匹配: +1 415-555-2671 ext 1234不匹配: not-a-phone

Digits Only (Phone)

^\d{10,15}$

Matches phone numbers as digit-only strings (10 to 15 digits)

匹配: 4155552671不匹配: 12345

IP 与网络

IPv4 Address

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Matches a valid IPv4 address (0.0.0.0 to 255.255.255.255)

匹配: 192.168.1.1不匹配: 256.1.1.1

IPv4 with Port

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):(\d{1,5})$

Matches an IPv4 address followed by a port number

匹配: 192.168.1.1:8080不匹配: 192.168.1.1

IPv6 Address

^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

Matches a full IPv6 address (8 groups of hex digits)

匹配: 2001:0db8:85a3:0000:0000:8a2e:0370:7334不匹配: 192.168.1.1

MAC Address

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$

Matches a MAC address with colon or dash separators

匹配: 00:1A:2B:3C:4D:5E不匹配: 001A2B3C4D5E

CIDR Notation

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)$

Matches an IPv4 address with CIDR subnet notation

匹配: 192.168.1.0/24不匹配: 192.168.1.0/33

文件与路径

File Extension

\.([a-zA-Z0-9]+)$

Extracts the file extension from a filename

匹配: document.pdf不匹配: no-extension

Unix File Path

^\/(?:[^\/ ]+\/)*[^\/ ]+$

Matches an absolute Unix/Linux file path

匹配: /usr/local/bin/node不匹配: relative/path

Windows File Path

^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$

Matches an absolute Windows file path

匹配: C:\Users\docs\file.txt不匹配: /usr/local/bin

Image File

\.(jpe?g|png|gif|bmp|svg|webp|ico|tiff?)$

Matches filenames with common image extensions

匹配: photo.jpg不匹配: script.js

密码

Min 8 Characters

^.{8,}$

Matches any string with at least 8 characters

匹配: password123不匹配: short

Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Requires uppercase, lowercase, digit, and special character (min 8 chars)

匹配: P@ssw0rd!不匹配: password

No Spaces

^\S+$

Matches strings that contain no whitespace characters

匹配: no-spaces-here不匹配: has spaces

Alphanumeric Only

^[a-zA-Z0-9]+$

Matches strings containing only letters and digits

匹配: abc123不匹配: hello world!

HTML 与 XML

HTML Tag

<\/?[a-zA-Z][a-zA-Z0-9]*[^>]*>

Matches opening and closing HTML tags

匹配: <div class="test">不匹配: plain text

HTML Comment

<!--[\s\S]*?-->

Matches HTML comments including multiline content

匹配: <!-- comment -->不匹配: // js comment

HTML Attribute

([a-zA-Z-]+)\s*=\s*"([^"]*)"

Extracts attribute name and value pairs from HTML elements

匹配: class="active"不匹配: no-attribute

Self-Closing Tag

<[a-zA-Z][a-zA-Z0-9]*[^>]*\/>

Matches self-closing HTML/XML tags like <br /> or <img />

匹配: <img src="test.png" />不匹配: <div>content</div>

杂项

UUID v4

^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

Matches a UUID version 4 string (lowercase hex)

匹配: 550e8400-e29b-41d4-a716-446655440000不匹配: not-a-uuid

JWT Token

^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$

Matches the three-part structure of a JSON Web Token

匹配: eyJhbGci.eyJzdWIi.SflKxwRJ不匹配: not.a.jwt.token

Markdown Link

\[([^\]]+)\]\(([^)]+)\)

Matches Markdown-style links [text](url)

匹配: [Google](https://google.com)不匹配: plain text link

Semantic Version

^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*))?$

Matches semantic version strings like 1.2.3 or v2.0.0-beta.1

匹配: v2.1.0-beta.1不匹配: 1.2

Base64 String

^[A-Za-z0-9+/]*={0,2}$

Matches valid Base64-encoded strings

匹配: SGVsbG8gV29ybGQ=不匹配: Hello World!!!

了解更多

正则库:你真正会用到的常见模式

浏览精选的正则模式集合,涵盖邮箱、URL、日期等。

什么是正则表达式库?

正则表达式库是一份经过精心整理的模式集合,用于处理常见的校验和提取任务。你无需每次都从头编写正则表达式,而是可以直接浏览针对电子邮件、URL、日期、IP 地址等数十种格式、经过测试的现成模式。

阅读约 5 分钟阅读完整指南

常见问题