20 Essential Regular Expressions for Common 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, images, links, CSS properties, comments, and HTML tags, each illustrated with examples and images.
Regular expressions are a powerful and efficient text‑processing tool that allow developers to implement complex validation logic concisely and quickly.
1. Password Strength Validation
The password must contain a mix of uppercase and lowercase letters and digits, exclude special characters, and be 8‑10 characters long.
2. Chinese Character Validation
The string may contain only Chinese characters.
3. Alphanumeric and Underscore Validation
Matches strings composed of digits, the 26 English letters, or underscores.
4. E‑Mail Address Validation
Regular expression that checks the compliance of an e‑mail address, similar to the password rule.
5. Identity Card Number Validation
Validates Chinese ID numbers, supporting both 15‑digit and 18‑digit formats.
6. Date Validation (yyyy‑mm‑dd)
Checks dates in the format yyyy‑mm‑dd, accounting for leap years.
7. Monetary Amount Validation
Validates amounts with up to two decimal places.
8. Mobile Phone Number Validation
Matches Chinese mobile numbers starting with 13, 15, or 18 (extendable to other prefixes).
9. Internet Explorer Version Detection
Regular expression for checking the IE browser version to handle compatibility.
10. IPv4 Address Validation
Regex pattern for validating IPv4 addresses.
11. IPv6 Address Validation
Regex pattern for validating IPv6 addresses.
12. URL Prefix Check
Extracts the protocol prefix (http or https) from a URL for further logic.
13. Extract URLs from Text
Finds URLs embedded in a block of text.
14. File Path and Extension Validation
Validates Windows file paths and extensions (example shown for .txt files).
15. Extract Color Hex Codes
Regular expression for extracting hexadecimal color codes from web pages.
16. Extract Images from Web Pages
Finds all image URLs within an HTML document.
17. Extract Hyperlinks
Extracts anchor tag href attributes from HTML.
18. Find CSS Properties
Searches for matching CSS property definitions.
19. Extract HTML Comments
Removes or extracts HTML comments from source code.
20. Match HTML Tags
Matches HTML tag attributes within markup.
Regex Syntax Cheat Sheet
A handy cheat sheet summarizing common regular‑expression syntax for quick reference.
Learning Regular Expressions
A recommended guide for beginners who want to deepen their understanding of regular expressions.
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.
