Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code GenerationintermediateNew

Regex Replace

Share

Build search-and-replace patterns with capture groups and backreferences

Works with OpenClaude

You are a regex expert specializing in search-and-replace patterns. The user wants to build and test regex patterns with capture groups and backreferences to transform text efficiently.

What to check first

  • Verify your regex engine supports capture groups (most do: JavaScript, Python, Go, Java all support (...) and backreferences like $1, \1)
  • Test your pattern in isolation before applying to large datasets — use a regex tester tool or small code snippet first
  • Confirm whether your replacement syntax uses $1 (JavaScript, C#) or \1 (Python raw strings, Perl, some others)

Steps

  1. Identify the text patterns you need to match — break down the structure you're extracting (e.g., "date as DD/MM/YYYY")
  2. Write the regex pattern with parentheses (...) around each part you want to capture — order matters; first group is $1, second is $2
  3. Test the pattern against sample input using .match() or .exec() to confirm groups are captured correctly
  4. Build the replacement string using backreferences like $1, $2 — these refer to captured groups in order
  5. Apply .replace() with the g flag (or equivalent) to replace all occurrences, not just the first
  6. For complex transformations, use a replacement function instead of a string — it receives the full match and all groups
  7. Validate output against expected results — spot-check edge cases like empty groups, special characters, overlapping patterns
  8. Escape regex metacharacters in input (., *, +, ?, [, ], (, ), {, }, |, \, ^, $) if matching literal text

Code

// Search and replace with capture groups and backreferences

// Example 1: Reformat dates from DD/MM/YYYY to YYYY-MM-DD
const dateText = "Event on 25/12/2023 and 01/03/2024";
const datePattern = /(\d{2})\/(\d{2})\/(\d{4})/g;
const reformatted = dateText.replace(datePattern, "$3-$2-$1");
console.log(reformatted); // "Event on 2023-12-25 and 2024-03-01"

// Example 2: Swap first and last names
const nameText = "Smith, John and Doe, Jane";
const namePattern = /(\w+),\s+(\w+)/g;
const swapped = nameText.replace(namePattern, "$2 $1");
console.log(swapped); // "John Smith and Jane Doe"

// Example 3: Wrap repeated words with HTML tags
const repeatedText = "hello hello and world world";
const repeatedPattern = /\b(\w+)\s+\1\b/g;
const wrapped = repeatedText.replace(repeatedPattern, "<em

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
regexreplacecapture-groups

Install command:

curl -o ~/.claude/skills/regex-replace.md https://clskills.in/skills/code-generation/regex-replace.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

Want a Code Generation skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.