Real-World Examples
Code Refactoring

Code Refactoring

Regex is invaluable for large-scale code refactoring. Whether you're renaming variables, updating import statements, or migrating APIs, these patterns will help.

Variable and Function Renaming

Simple Rename

Find all occurrences of a variable name (as a whole word):

\boldName\b

The \b word boundaries ensure you match oldName but not oldNameSuffix or prefixOldName.

Try it → (opens in a new tab)

Rename with Context

Find variable declarations specifically:

\b(const|let|var)\s+oldName\b

Try it → (opens in a new tab)

Rename Object Property Access

\.oldProperty\b

Try it → (opens in a new tab)

Import Statement Updates

ES6 Import Refactoring

Find imports from a specific module:

import\s+(?:{[^}]+}|\w+)\s+from\s+['"]@old-package['"]

Try it → (opens in a new tab)

Extract Named Imports

import\s+{\s*(?<imports>[^}]+)\s*}\s+from\s+['"](?<module>[^'"]+)['"]

Try it → (opens in a new tab)

CommonJS to ES6

Find require statements to convert:

const\s+(?<name>\w+)\s*=\s*require\(['"](?<module>[^'"]+)['"]\)

Try it → (opens in a new tab)

API Migration

Update Method Calls

Migrate from old API to new:

oldApi\.oldMethod\(([^)]*)\)

Replace with: newApi.newMethod($1)

Try it → (opens in a new tab)

Update Callback to Promise

Find callback-style code:

(\w+)\(([^,]+),\s*function\s*\((?:err,\s*)?(\w+)\)\s*{

Try it → (opens in a new tab)

React Class to Hooks

Find this.state references:

this\.state\.(\w+)

Find this.setState:

this\.setState\(\{([^}]+)\}\)

Try it → (opens in a new tab)

String and Comment Updates

Update String Literals

Find strings containing specific text:

(['"`])([^'"`]*old-text[^'"`]*)\1

Try it → (opens in a new tab)

Find TODO Comments

\/\/\s*TODO:?\s*(.*)

Try it → (opens in a new tab)

Find FIXME and HACK Comments

\/\/\s*(TODO|FIXME|HACK|XXX):?\s*(.*)

Type Annotations

TypeScript Type Updates

Find type annotations:

:\s*OldType\b

Find generic type parameters:

<OldType(?:\s*,\s*\w+)*>

Try it → (opens in a new tab)

Find Interface Properties

(\w+):\s*OldType;

Practical Workflow

⚠️

Always use version control! Before running find-and-replace across your codebase, commit your changes so you can easily revert if something goes wrong.

Step-by-Step Refactoring

  1. Write the pattern in RegexLens
  2. Test with samples from your actual codebase
  3. Check the explanation to ensure it matches what you expect
  4. Review warnings for potential issues
  5. Run find-and-replace in your IDE
  6. Run tests to verify the refactoring

IDE Integration

Most IDEs support regex find-and-replace:

  • VS Code: Ctrl/Cmd + H, click .* to enable regex
  • IntelliJ: Ctrl/Cmd + R, check "Regex" checkbox
  • Sublime Text: Ctrl/Cmd + H, enable regex mode

Capture Groups for Replacement

Use capture groups to preserve parts of the match:

Find:

console\.log\((['"`][^'"`]+['"`])\)

Replace:

logger.info($1)

This converts console.log('message') to logger.info('message').

Common Refactoring Patterns

TaskFind PatternReplace
Rename variable\boldName\bnewName
Update import pathfrom ['"]@old/from '@new/
Add prefix to function\bfunctionName\(prefix_functionName(
Convert var to const\bvar\s+const
Remove console.logconsole\.log\([^)]*\);?\n?(empty)