Skip to content
Plurafy

Regular Expressions

Mastering regex patterns for string matching and manipulation

You need to validate email addresses, extract data from strings, and transform text patterns. How do you write efficient regex patterns and apply them correctly?

Regular expressions are powerful patterns for matching and manipulating strings. Understanding regex syntax, methods, and performance implications is essential for text processing.

// Create regex patterns
const pattern1 = /hello/i; // Literal notation
const pattern2 = new RegExp('hello', 'i'); // Constructor
// Test if pattern matches
console.log(/test/.test('testing')); // true
console.log(/test/.test('hello')); // false
// Find matches
const matches = 'hello world'.match(/\w+/g);
console.log(matches); // ['hello', 'world']
// Replace
const result = 'hello world'.replace(/world/, 'there');
console.log(result); // 'hello there'
// Email validation (simple)
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(emailPattern.test('user@example.com')); // true
console.log(emailPattern.test('invalid.email')); // false
// Extract information from string
const logLine = '2024-01-15 10:30:45 ERROR Connection failed';
const timePattern = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
const match = logLine.match(timePattern);
if (match) {
const [full, year, month, day, hour, min, sec] = match;
console.log({ year, month, day, hour, min, sec });
}
// Extract all email addresses from text
const text = 'Contact admin@example.com or support@example.com';
const emails = text.match(/[^\s@]+@[^\s@]+\.[^\s@]+/g);
console.log(emails); // ['admin@example.com', 'support@example.com']
// Replace with captured groups
const name = 'Doe, John';
const formatted = name.replace(/(\w+),\s*(\w+)/, '$2 $1');
console.log(formatted); // 'John Doe'
const text = 'Hello HELLO hello';
// i: Case insensitive
console.log(text.match(/hello/i)); // ['Hello']
console.log(text.match(/hello/gi)); // ['Hello', 'HELLO', 'hello']
// g: Global (find all)
console.log('cat cat cat'.match(/cat/g)); // ['cat', 'cat', 'cat']
console.log('cat cat cat'.match(/cat/)); // ['cat']
// m: Multiline (^ and $ match line boundaries)
const multiline = 'line1\nline2';
console.log(multiline.match(/^line/m)); // ['line'] - matches first line start
console.log(multiline.match(/^line/gm)); // ['line', 'line'] - matches all line starts
// s: Dotall (. matches newlines)
console.log('a\nb'.match(/a.b/)); // null
console.log('a\nb'.match(/a.b/s)); // ['a\nb']
// Character classes
/[abc]/ // a, b, or c
/[^abc]/ // NOT a, b, or c
/[a-z]/ // a to z
/[0-9]/ // 0 to 9
/\d/ // digit
/\D/ // NOT digit
/\w/ // word character
/\W/ // NOT word character
/\s/ // whitespace
/\S/ // NOT whitespace
// Quantifiers
/a*/ // 0 or more a's
/a+/ // 1 or more a's
/a?/ // 0 or 1 a
/a{3}/ // exactly 3 a's
/a{3,}/ // 3 or more a's
/a{3,5}/ // 3 to 5 a's
// Practical patterns
const ipAddress = /^(\d{1,3}\.){3}\d{1,3}$/;
const phoneNumber = /^\+?1?\s?(\d{3})[-.\s]?(\d{3})[-.\s]?(\d{4})$/;
const url = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
// split
const csv = 'John,Doe,30,john@example.com';
const fields = csv.split(/,/);
console.log(fields); // ['John', 'Doe', '30', 'john@example.com']
// replace with callback
const result = 'hello world'.replace(/\w+/g, (match) => {
return match.toUpperCase();
});
console.log(result); // 'HELLO WORLD'
// replace with groups
const date = '2024-01-15';
const formatted = date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1');
console.log(formatted); // '15/01/2024'
// replaceAll (modern)
const text = 'foo foo foo';
const replaced = text.replaceAll('foo', 'bar');
console.log(replaced); // 'bar bar bar'
// search - get index
console.log('hello world'.search(/world/)); // 6
// ✅ Compile regex once
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const emails = ['user@example.com', 'admin@example.com'];
emails.forEach(email => emailRegex.test(email));
// ❌ Avoid: Compiling in loop
emails.forEach(email => {
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { }
});
// ❌ Avoid: Catastrophic backtracking
// This can cause performance issues
const bad = /(x+x+)+y/; // Nested quantifiers
// ✅ Better: Use simpler patterns
const good = /x+y/;
// Test performance
const pattern = /^[a-zA-Z0-9]{3,20}$/;
const testString = 'valid-username';
console.time('regex-test');
for (let i = 0; i < 1000000; i++) {
pattern.test(testString);
}
console.timeEnd('regex-test');