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:
Find: (\w+)\s*=\s*(\w+)
Replace: $2 = $1Press 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.
| Pattern | Matches | Replace with | Result |
|---|---|---|---|
| (\w+).(\w+) | user.name | $2.$1 | name.user |
| "(\w+)": | "title": | $1: | title: |
(\d{2})/(\d{2})/(\d{4}) | 01/08/2026 | $3-$2-$1 | 2026-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:
| Modifier | Effect | Example |
|---|---|---|
| \u$1 | Uppercase next character | hello becomes Hello |
| \U$1 | Uppercase rest of capture | hello becomes HELLO |
| \l$1 | Lowercase next character | Hello becomes hello |
| \L$1 | Lowercase rest of capture | HELLO 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.
Multiline search
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:
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
| Task | Find pattern | Replace |
|---|---|---|
| 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
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.
Frequently Asked Questions
How do I replace with a newline using regex?
Why does my regex work on regex101.com but not in VS Code?
Can I use regex in Search Across Files too?
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).
More in this topic
How to Use VS Code with WSL 2 on Windows
Run VS Code connected to Windows Subsystem for Linux so you can develop in a full Linux environment with native tools, terminals, and debugging, all from Windows.
20 Best VS Code Extensions for Web Developers in 2026
Twenty carefully chosen VS Code extensions every web developer should know. Covers formatting, linting, frameworks, debugging, Git, and developer experience.
How to Install, Disable, Update, and Uninstall VS Code Extensions
Learn how to install, disable, update, and uninstall VS Code extensions from the Marketplace and the command line. Step-by-step instructions for every action.