Advanced Regex Techniques

Advanced Regex Techniques

Backreferences

Backreferences allow you to reuse a part of the regex match in the pattern itself or in the replacement string. This is useful for tasks such as finding repeated substrings.

Example: Using Backreferences

  1. In the Pattern
javascript
	const regex = /(\b\w+\b) \1/;
	console.log(regex.test('hello hello')); // true (matches repeated word 'hello')
	console.log(regex.test('hello world')); // false (no match because words are different)

Explanation:

  • (\b\w+\b) : Captures a whole word.
  • \1 : Refers to the first captured group, ensuring the same word follows.
  1. In the Replacement String
javascript
	const str = 'apple banana apple';
	const regex = /(\b\w+\b) \1/;
	const newStr = str.replace(regex, '$1');
	console.log(newStr); // 'apple banana' (removes the repeated 'apple')

Conditional Patterns

Conditional patterns allow you to apply different regex patterns based on a condition. This feature is supported in some regex flavors but not all.

Example: Conditional Patterns

javascript
	// Example syntax for a hypothetical regex engine supporting conditionals
	const regex = /(foo)?bar(?(1)baz|qux)/;
	console.log(regex.test('foobar')); // false (no match because 'bar' is not followed by 'baz')
	console.log(regex.test('foobarbaz')); // true (matches because 'bar' is followed by 'baz')
	console.log(regex.test('barqux')); // true (matches because 'bar' is followed by 'qux')

Explanation:

  • (foo)? : Optionally matches ‘foo’.
  • bar : Matches ‘bar’.
  • (?(1)baz|qux) : If the first group (foo) is matched, it expects ‘baz’ to follow ‘bar’. Otherwise, it expects ‘qux’.

Example: Matching Palindromes

A palindrome is a word that reads the same forward and backward. You can use backreferences to match palindromes.

javascript
	const regexPalindrome = /^(\w)(\w)?(\w)?\w?\3\2\1$/;
	console.log(regexPalindrome.test('racecar')); // true (matches 'racecar')
	console.log(regexPalindrome.test('level')); // true (matches 'level')
	console.log(regexPalindrome.test('hello')); // false (no match)

Explanation:

  • ^(\w) : Captures the first character.
  • (\w)? : Optionally captures the second character.
  • (\w)? : Optionally captures the third character.
  • \w? : Optionally matches the middle character (for odd-length palindromes).
  • \3\2\1 : Refers to the captured groups in reverse order.
  • $ : Ensures the match ends at the end of the string.

Exercises

  1. Write a regular expression to find and replace repeated words in a string with a single instance of the word.
  2. Create a Regex pattern to validate a string as a palindrome.
  3. Write a regular expression that uses a conditional pattern to match a word only if it is followed by a specific suffix.

Backreferences and conditional patterns add significant power and flexibility to your regular expressions, allowing for complex matching and replacement scenarios.