Fundamentals 8 min read

Essential Regex Cheat Sheet: 20 Common Validation Patterns for Developers

This cheat sheet compiles twenty practical regular‑expression patterns—including date, password, email, ID, IP, URL, and HTML validation examples—along with JavaScript snippets, giving developers a quick reference to boost input‑checking efficiency across front‑end and back‑end projects.

21CTO
21CTO
21CTO
Essential Regex Cheat Sheet: 20 Common Validation Patterns for Developers

1. Date format validation (JavaScript)

Validate a basic date format using a regular expression and alert on error.

var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
var r = fieldValue.match(reg);
if (r == null) alert('Date format error!');

2. Password strength

The password must contain digits, lowercase and uppercase letters, be 8‑10 characters long, and exclude special symbols.

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

3. Chinese characters only

Ensures the entire string consists of Chinese characters.

^[\u4e00-\u9fa5]{0,}$

4. Alphanumeric or underscore

Matches strings composed of letters, digits, or underscores.

^\w+$

5. Email address

Comprehensive pattern for validating standard email formats.

[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?

6. 15‑digit ID number

^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$

7. 18‑digit ID number

^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$

8. Full date validation (yyyy‑mm‑dd, leap year aware)

^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$

9. Monetary amount (up to two decimal places)

^[0-9]+(\.[0-9]{2})?$

10. Mobile phone number (China)

^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$

11. IPv4 address

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

12. IPv6 address

(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,5}(:[0-9a-fA-F]{1,4}){1,2})|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,3}(:[0-9a-fA-F]{1,4}){1,4})|([0-9a-fA-F]{1,2}(:[0-9a-fA-F]{1,4}){1,5})|([0-9a-fA-F]{1}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))

13. Ensure URL has protocol prefix

if (!s.match(/^[a-zA-Z]+:\/\//)) { s = 'http://' + s; }

14. Extract URL

^(f|ht){1}(tp|tps):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?

15. File path and .txt extension

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

16. Color hex code

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

17. Extract image source URL

\< *[img][^>]*[src] *= *["']{0,1}([^"' >]*)

18. Extract hyperlink (excluding certain domains)

(<a\s*(?!.*\brel=)[^>]*)(href="https?:\/\/)((?!(?:(?:www\.)?'.implode('|(?:www\.)?', $follow_list).'))[^"]+)("((?!.*\brel=)[^>]*)(?:[^>]*)>)

19. CSS property declaration

^\s*[a-zA-Z\-]+\s*[:]{1}\s[a-zA-Z0-9\s.#]+[;]{1}

20. HTML comment

<!--(.*?)-->

21. Match any HTML tag

<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'"&>\s]+))?)+\s*|\s*)\/?>

The cheat sheet concludes with an illustrative image and attribution to the original author.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendfrontendJavaScriptvalidationregular expressionsregexpattern-matching
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.