Regex Substitution: Transforming Text with Patterns

Explore how to use regex substitution to replace matched text with new strings. Learn about replacement patterns and special characters for referring to captured groups and transforming text data effectively.



Substitution in Regular Expressions

Substitution is the process of replacing matched text with a new string based on a regular expression pattern. It's a powerful tool for transforming text data.

Replacement Patterns

Substitution often involves using special characters in the replacement string to refer to captured groups or other parts of the match:

  • $n: Includes the last substring matched by the capturing group identified by n, where n is a decimal value.
  • ${name}: Includes the last substring matched by the named capturing group designated by (?<name>...).
  • $$: Includes a single $ literal in the replacement string.
  • $&: Includes a copy of the entire match in the replacement string.
  • $`: Includes all the text of the input string before the match in the replacement string.
  • $': Includes all the text of the input string after the match in the replacement string.
  • $+: Includes the last group captured in the replacement string.
  • $_: Includes the entire input string in the replacement string.

Examples

Example 1: Basic Substitution

JavaScript
const str = 'Hello, world!';
const newStr = str.replace(/Hello/, 'Hi');
console.log(newStr); // Output: Hi, world!

This example replaces 'Hello' with 'Hi'.

Example 2: Using Backreferences

JavaScript
const str = '123-456-7890';
const newStr = str.replace(/(\d{3})-(\d{3})-(\d{4})/, '$2-$1-$3');
console.log(newStr); // Output: 456-123-7890

This example rearranges the parts of a phone number.

Example 3: Using Special Characters

JavaScript
const str = 'The quick brown fox jumps over the lazy dog.';
const newStr = str.replace(/\bfox\b/, '$& and the cat');
console.log(newStr); // Output: The quick brown fox and the cat jumps over the lazy dog.

This example adds 'and the cat' after 'fox'.

Common Use Cases

  • Formatting data: Converting date formats, phone numbers, etc.
  • Extracting information: Isolating specific parts of text.
  • Sanitizing input: Removing or modifying potentially harmful characters.
  • Creating new text: Generating text based on patterns.

Additional Notes

  • Different programming languages might have slight variations in syntax and available substitution features.
  • For complex substitutions, consider using named capturing groups to improve readability.
  • Be aware of potential security implications when using user-provided input in substitution patterns.