How to Find and Replace Text with Regular Expressions in VS Code

Use regex in VS Code to find and replace complex text patterns. Learn capture groups, backreferences, case modifiers, multiline search, and find in selection.

5 min read

VS Code has a full regular expression engine built into its Find and Replace control. You can search for patterns instead of exact text, capture parts of matches, and rearrange them in the replacement.

Regex is useful whenever find and replace is too repetitive to do by hand: swapping the order of function arguments, changing date formats, wrapping values in quotes, or removing debug logging from a whole file.

Open find and replace

Press Ctrl+F (Windows/Linux) or Cmd+F (macOS) to open the Find control at the top right of the editor.

Press Ctrl+H (Windows/Linux) or Cmd+Opt+F (macOS) to open it with the Replace field already visible.

The Find control has toggle buttons for Match Case, Match Whole Word, and regular expressions. Click the .* icon or press Alt+R to enable regex mode. A checkmark appears on the icon when regex is active.

As you type a regex pattern, matches are highlighted in the editor. Press Enter to jump to the next match. Press Shift+Enter to go to the previous one.

Your first regex replace

Open any file and enable regex mode. Type this pattern in the Find field and this replacement in the Replace field:

texttext
Find:    (\w+)\s*=\s*(\w+)
Replace: $2 = $1

Press Ctrl+Alt+Enter to replace all, or press Enter to preview each match individually.

This pattern finds assignments like width = 100 and swaps them to 100 = width. The parentheses create capture groups: $1 is the first group (before the =), and $2 is the second (after the =).

Capture groups and backreferences

Enclose parts of your pattern in parentheses to capture them. Reference captured text in the replacement with $1, $2, and so on. $0 refers to the entire match.

PatternMatchesReplace withResult
(\w+).(\w+)user.name$2.$1name.user
"(\w+)":"title":$1:title:
(\d{2})/(\d{2})/(\d{4})01/08/2026$3-$2-$12026-08-01

To insert a literal $ character in the replacement, type two dollar signs together: $$.

Change case in replacements

VS Code supports case modifiers that transform captured text during replacement:

ModifierEffectExample
\u$1Uppercase next characterhello becomes Hello
\U$1Uppercase rest of capturehello becomes HELLO
\l$1Lowercase next characterHello becomes hello
\L$1Lowercase rest of captureHELLO becomes hello

These can be stacked. \u\U$1 uppercases the first character and the rest, turning hello world into HELLO WORLD.

Practical example: to convert snake_case identifiers to PascalCase, find _(\w) (a single character after an underscore) and replace with \U$1 (the captured character uppercased).

Find and replace in a selection

Select the lines you want to search within. Press Alt+L (Windows/Linux) or Cmd+Opt+L (macOS), or click the Find in Selection icon (three lines with a magnifying glass) in the Find control.

Only matches inside the selection are found and highlighted. The rest of the file is ignored. This is useful when you want to rename variables inside one function without touching the rest of the file.

To make Find in Selection the default behavior, set editor.find.autoFindInSelection to always in your settings.

Press Ctrl+Enter inside the Find or Replace input box to insert a newline. This lets you search for patterns that span multiple lines.

For example, to find a function definition followed by its opening brace, with regex enabled:

texttext
Find:    function (\w+)\(
Replace: export function $1(

This adds export to function declarations without touching function calls.

Drag the left edge of the Find control to resize it if the multiline text is hard to read.

Useful regex patterns

TaskFind patternReplace
Remove trailing whitespace\s+$(empty)
Remove empty lines^\s*\n(empty)
Wrap CSV values in quotes([^,]+)"$1"
Add comma to end of line$,
Remove line-start numbers^\d+.\s*(empty)
Swap first and last name(\w+),\s*(\w+)$2 $1

Replace across files

The same regex engine works in cross-file search. Press Ctrl+Shift+F (Windows/Linux) or Cmd+Shift+F (macOS) to open the Search view.

Type your regex pattern in the search box. Click the .* toggle to enable regex. Click the arrow next to the search box to show the Replace field. You see a diff preview of changes before applying them across all files.

Troubleshooting

If your regex matches nothing when you expect it to, check three things: the .* icon is highlighted (regex is on), Match Case (the Aa icon) is set correctly, and you are not accidentally in Find in Selection mode. VS Code regex in the editor Find control is case-insensitive by default, which is different from many online regex testers.

To review pending replacements safely, use the Replace button (not Replace All) to step through each match. Undo with Ctrl+Z (Cmd+Z) if a replacement does not produce what you expected. For more on VS Code search features, see the keyboard shortcuts cheat sheet. If you need to make the same edit across many locations, the multiple cursors guide shows another approach.

Rune AI

Rune AI

Key Insights

  • Press Ctrl+F (Cmd+F) to open Find, Ctrl+H (Cmd+Opt+F) for Find and Replace. Click the .* icon or press Alt+R to enable regex mode.
  • Capture groups use parentheses (pattern). Reference them in the replacement with $1, $2, etc. Use $0 for the full match.
  • Change case in replacements: \u uppercases the next character, \U uppercases the rest, \l lowercases the next, \L lowercases the rest.
  • Select text first, then press Alt+L to toggle Find in Selection. Only matches within the selection are found.
  • Press Ctrl+Enter in the find or replace field to insert a newline for multiline search.
RunePowered by Rune AI

Frequently Asked Questions

How do I replace with a newline using regex?

In the replace field, use \n to insert a newline. For example, replace ; with ;\n to put each statement on its own line. In the find field, use \n to match newlines when regex mode is on.

Why does my regex work on regex101.com but not in VS Code?

VS Code uses a JavaScript regex engine in the single-file Find control, but the Search view (Ctrl+Shift+F) searches with ripgrep and falls back to PCRE2 for patterns ripgrep cannot parse, such as lookbehind and backreferences. A few differences follow from this: lookbehind works fully in the single-file Find control, but only fixed-length lookbehind works reliably in Search across files. Also, VS Code defaults to case-insensitive matching -- toggle Match Case (Alt+C) if you need case sensitivity.

Can I use regex in Search Across Files too?

Yes. Press Ctrl+Shift+F (Cmd+Shift+F) to open search across files. Enable the regex toggle (the .* icon) and write your pattern. The replace across files works the same way with capture groups and case modifiers.

Conclusion

Regex find and replace turns pattern-based edits into one operation. Enable regex with Alt+R, use parentheses to capture groups, and reference them with $1, $2 in the replacement. Limit searches to a selection, work across multiple lines, and use \u and \U to change case in replacements. The same regex controls work in both single-file find (Ctrl+F) and project-wide search (Ctrl+Shift+F).