Master Common Regex Patterns: Digits, Times, IPs, and HTML Tags
This article walks through practical regular‑expression exercises, explains each solution with clear JavaScript examples, and then expands to real‑world patterns for matching dollar amounts, 24‑hour times, IP addresses, quoted strings, and HTML tags, offering tips for building robust regexes.
Simple Regex Exercises
A set of six practice tasks covering: (1) matching the first three digits at the start of a string, (2) matching any character except a, b or c, (3) the result of '1234567'.match(/\d{1,3}/g), (4) matching words that do not begin with th, (5) a password rule of 4‑8 characters with at least one digit, and (6) matching a single Chinese character.
Answers
Match three digits at the start: /^\d{3}/ Match any character except a, b, c: /[^abc]/ Result of the greedy match: ["123", "456", "7"] Match words not beginning with th: /\b(?!th)\w+\b/ Password rule (4‑8 chars, at least one digit): /^(?=.*\d).{4,8}$/ Match a Chinese character:
/[\u4e00-\u9fa5]/Principles of Good Regexes
Match only the desired text and exclude unwanted text.
Keep the expression easy to understand and maintain.
Ensure efficiency.
Matching Dollar Amounts
The pattern /^\$[0-9]+(\.[0-9][0-9])?$/ matches a leading dollar sign, one or more digits, and an optional decimal part with exactly two digits. It does not match formatted amounts such as $1,000.
Matching 24‑Hour Time (e.g., 09:59)
Two equivalent approaches: /\b(?:[01]?[0-9]|2[0-3]):[0-5][0-9]\b/ or /\b(?:[012]?[0-3]|[01]?[4-9]):[0-5][0-9]\b/ For full‑string validation replace the leading \b with ^ and the trailing \b with $. When used for search‑replace be aware that the pattern may also match substrings like "19:50" inside longer text.
Matching IPv4 Addresses
A field representing a number from 0 to 255 can be expressed as: [01]?\d\d?|2[0-4]\d|25[0-5] Combining four such fields yields the full address regex:
/^(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])$/To reject the special case 0.0.0.0 add a negative look‑ahead:
/^(?!0+\.0+\.0+\.0+$)(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])$/Matching Text Between Delimiters
Quoted Strings Allowing Escaped Quotes
A robust pattern that handles escaped quotes inside double‑quoted strings:
/"(?:\\.|[^\\"])*"/HTML Tag Matching
Pattern that matches an HTML tag where attribute values may be double‑quoted, single‑quoted, or unquoted, and allows the > character inside attribute values:
/<(?:"[^"]*"|'[^']*'|[^'">])*>/Reference
Mastering Regular Expressions – http://imweb.io/topic/56e804ef1a5f05dc50643106
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.
Aotu Lab
Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.
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.
