20 Essential Regular Expressions for Everyday Validation Tasks
This article presents a curated collection of twenty practical regular expressions covering password strength, Chinese characters, alphanumeric strings, email, ID numbers, dates, monetary values, phone numbers, browser versions, IP addresses, URLs, file paths, color codes, image extraction, hyperlink extraction, CSS properties, comments, and HTML tags, each illustrated with clear examples to boost development efficiency.
Regular expressions (regex) are powerful text‑processing tools that enable concise and efficient implementation of complex validation logic.
1. Validate Password Strength
The password must be a combination of uppercase and lowercase letters and digits, exclude special characters, and be 8‑10 characters long.
2. Validate Chinese Characters
The string must consist solely of Chinese characters.
3. Alphanumeric or Underscore String
A string composed of digits, the 26 English letters, or underscores.
4. Validate E‑Mail Address
Standard regex for checking email address compliance.
5. Validate ID Card Number
Regex for Chinese ID numbers, supporting both 15‑ and 18‑digit formats.
6. Validate Date (yyyy‑mm‑dd)
Regex that checks dates in the "yyyy‑mm‑dd" format, accounting for leap years.
7. Validate Monetary Amount
Amount validation accurate to two decimal places.
8. Validate Mobile Phone Number
Regex for domestic phone numbers starting with 13, 15, or 18 (extendable for other prefixes).
9. Detect IE Version
Regex for checking Internet Explorer versions to handle compatibility.
10. Validate IPv4 Address
Standard regex for IPv4 addresses.
11. Validate IPv6 Address
Standard regex for IPv6 addresses.
12. Check URL Prefix
Regex to extract the protocol prefix (http or https) from a URL for further logic.
13. Extract URLs from Text
Regex that finds URLs within a block of text.
14. Validate File Path and Extension
Regex for Windows file paths and specific extensions (e.g., .txt).
15. Extract Color Hex Codes
Regex to capture hexadecimal color codes from web pages.
16. Extract Images from Web Pages
Regex for pulling all image URLs from HTML content.
17. Extract Hyperlinks
Regex to find anchor tags and retrieve hyperlink URLs.
18. Find CSS Properties
Regex that matches specific CSS property declarations.
19. Extract Comments
Regex for removing HTML comments from source code.
20. Match HTML Tags
Regex that captures HTML tag attributes.
Regular Expression Syntax Cheat Sheet
A handy cheat sheet summarizing common regex syntax for quick reference.
Learning Regular Expressions
A recommended guide for beginners who want to deepen their understanding of regex.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
