Struggling with regex syntax? AI writes the pattern and explains how it works.
The Prompt:
Create a regex pattern that matches: - US phone numbers in formats: (555) 123-4567, 555-123-4567, 5551234567 Provide: 1. The regex pattern 2. Explanation of each part 3. 5 test cases (3 valid, 2 invalid) 4. Code example in JavaScript
AI Output:
// Pattern:
/^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$/
// Explanation:
// ^\(? - Optional opening parenthesis
// (\d{3}) - 3 digits (area code)
// \)? - Optional closing parenthesis
// [-.\s]? - Optional separator
// etc...
// Test:
const regex = /^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$/;
console.log(regex.test('(555) 123-4567')); // true
