Understanding and Applying Regular Expressions
Regular expressions are used for pattern matching and string manipulation. They consist of characters and metacharacters that define search criteria.
Consider this example string:
let sample = "abcaa1bSS*bc%2ba-aba.asdvb4A_Acj556g__aaabbbbbb";
console.log(sample.replace("a", "X")); // Replaces first 'a' only
console.log(sample.replace(/a/, "X")); // Replaces first 'a' only
console.log(sample.replace(/a/g, "X")); // Replaces all 'a' globally
console.log(sample.replace(/a/gi, "X")); // Replaces all 'a' case-insensitively
console.log(sample.replace(/a+/gi, "X")); // Replaces sequences of 'a' with single 'X'
console.log(sample.replace(/ab/gi, "X")); // Replaces 'ab' globally case-insensitive
console.log(sample.replace(/[ab]/gi, "X")); // Replaces 'a' or 'b' individually
console.log(sample.replace(/[a-z]/gi, "X")); // Replaces any letter a-z
console.log(sample.replace(/\d+/gi, "X")); // Replaces digit sequences
console.log(sample.replace(/\w+/gi, "X")); // Replaces word character sequences
console.log(sample.replace(/\W+/gi, "X")); // Replaces non-word characters
Modifiers
g: Global matchingi: Case-insensitive matching
Quantifiers
+: One or more occcurrences(): Groups patterns[]: Character class (matches one character from set)^: Negation when first in character class|: Alternation (OR)
Escape Sequences
\d: Digits [0-9]\D: Non-digits [^0-9]\w: Word characters [A-Za-z0-9_]\W: Non-word characters [^A-Za-z0-9_]
Pattern Construction Regular expressions can validate various formats:
Email validation:
let emailPattern = /^[a-z][a-z0-9]{0,5}@[a-z0-9]{2,9}\.[a-z]{2,4}$/;
console.log(emailPattern.test("user@example.com"));
QQ number validation:
let qqPattern = /^[1-9]\d{4,11}$/;
console.log(qqPattern.test("123456789"));
Phone number validation:
let phonePattern = /^(0\d{2,3}-)?[1-9]\d{6}(-\d{1,4})?$/;
console.log(phonePattern.test("021-1234567"));
URL validation:
let urlPattern = /^(https?:\/\/)?([a-z\d]{1,6}\.)?[a-z\d]{2,18}(\.[a-z]{2,4}){1,2}$/;
console.log(urlPattern.test("www.example.com"));
Pattern Interpretation Understanding existing patterns requires analyzing components:
HTML tag removal:
let htmlPattern = /<[^<>]+>/gi;
let cleaned = htmlString.replace(htmlPattern, "");
Hexadecimal color validation:
let hexPattern = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/;
console.log(hexPattern.test("#ff0000"));
Complex email pattern analysis:
let complexEmail = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;
// Matches: letters/digits, optional dot-separated segments,
// @ symbol, domain with optional hyphens, and domain extensions
Common Validation Patterns
- Username:
/^[a-z0-9_-]{3,16}$/ - Password:
/^[a-z0-9_-]{6,18}$/ - IP Address:
/((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/ - Chinese characters:
/^[\u2E80-\u9FFF]+$/
Anchors
^: Start of string$: End of string Using anchors ensures complete string matching rather than partial matches.