Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 40 ยท Editing and Navigation

Find and Replace with Regular Expressions

Master VS Code Find and Replace and Search in Files: case, whole word and regex toggles, capture groups like $1, case modifiers, include/exclude globs and preview before replacing.

VS Code has two search tools: the Find widget for the current file and the Search view for the whole project. Both support regular expressions and replacement with captured groups. Learning them lets you do in seconds what would take an hour by hand, such as renaming a function in 200 files or reformatting a list.

Find in the current file

  • Ctrl+F opens Find; Ctrl+H opens Find and Replace.
  • Enter jumps to the next match, Shift+Enter to the previous one.
  • The counter (3 of 12) shows where you are; all matches are highlighted.
  • Alt+Enter selects all matches (so you can multi-cursor edit them, see Multi-Cursor Editing).
  • Ctrl+F with a word selected pre-fills the search with it.
  • Esc closes the widget.

Three toggle buttons sit at the right of the search box:

Toggle Shortcut Meaning
Aa Alt+C Match case
ab underlined Alt+W Match whole word
.* Alt+R Use regular expression

There is also Find in Selection (a small icon): it limits matches to the selected text, handy when you do not want to change a whole file.

In the Replace box: Enter replaces the current match, Ctrl+Alt+Enter replaces all. With Preserve Case (AB) the replacement keeps the capitalization of each match.

Search in files

Ctrl+Shift+F opens the Search view for the whole opened folder. The panel has:

  • the search box and replace box (Ctrl+Shift+H jumps straight to replace)
  • the same three toggles
  • files to include and files to exclude boxes
  • a results tree grouped by file; click a result to open it

The files to include field takes glob patterns separated by commas:

  • *.js any JavaScript file
  • src/**/*.ts TypeScript files anywhere under src
  • ./backend a folder, relative to the workspace root
  • !**/test/** (in the exclude box, no ! needed): **/test/** excludes tests

The search.exclude setting (by default node_modules, bower_components, .git) hides files from all searches; search.useIgnoreFiles makes search respect .gitignore. Use the Toggle Search Details (...) button to show the include/exclude boxes, or the icon Use Exclude Settings and Ignore Files.

Before replacing across files, always review the preview. Click the arrow next to the replace box (or the Preview option) to see each change as a diff, and remove matches you do not want with the X buttons. Replace All works on every remaining match; undo is available only per file, so commit to Git first and review changes with Source Control afterward.

Regular expressions in VS Code

VS Code uses JavaScript regular expression syntax. A few essentials:

Pattern Matches
. Any character (not newline)
\d \w \s Digit, word character, whitespace
^ $ Line start, line end
+ * ? One or more, zero or more, optional
{2,4} Between two and four
[abc] [^abc] One of / none of
(โ€ฆ) Capture group
(?:โ€ฆ) Non-capturing group
a|b Alternative
\b Word boundary
\n Newline (matches across lines in the widget)

In the replace box use $1, $2 ... for captured groups and $& or $0 for the whole match. You can also change case with modifiers, which VS Code adds on top of JavaScript: \u$1 uppercases the next character of group 1, \U$1 uppercases the whole group until \E, and \l, \L do lowercase.

Examples

Swap the order of two words: find (\w+), (\w+) replace $2 $1, so Lovelace, Ada becomes Ada Lovelace.

Turn snake_case into camelCase: find _(\w) replace \u$1, so user_first_name becomes userFirstName.

Wrap every number in quotes: find \b(\d+)\b replace "$1".

Remove trailing spaces: find [ \t]+$ and replace with nothing.

Delete blank lines: find ^\s*\n and replace with nothing (with regex on).

Change console.log(x) into logger.debug(x): find console\.log\((.*)\) replace logger.debug($1). Note the escaped dots and parentheses.

A stand-in: regex replace in JavaScript

VS Code's search runs the JavaScript regex engine, so you can rehearse a pattern on sample text before running it across a project. This stand-in uses String.replace with $1-style groups; the \u/\U case modifiers are a VS Code addition, which the stand-in implements by hand with a function.

JS Stand-in: practise find and replace

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The .*? in example 5 is lazy: it stops at the first ) it can, which matters when a line has two calls. A greedy .* would swallow from the first ( to the last ). VS Code shows matches live, so you can see this behavior as you type.

Turn regex on and use \n in the pattern to match across lines; the search box becomes multi-line when needed. For example, find \{\n\s+\} to locate empty blocks. Remember ^ and $ still match at line boundaries.

Search Editor

Search: Open New Search Editor opens your results in a normal editor tab, with context lines around each match. You can edit the query line at the top, keep it open while you work, and the .code-search file can be saved. This suits long refactoring sessions.

Tips

  1. Search selected text: select a word and press Ctrl+Shift+F; the view fills it in.
  2. Use Ctrl+K Ctrl+C / Ctrl+K Ctrl+U, not search, to comment and uncomment selections.
  3. Search is not semantic: for renaming a function, use F2 so only real references change.
  4. Test your regex on a small file first, then widen the scope.
  5. Commit before a big Replace All, so Git can show and undo what changed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut opens Search in files?

    Ctrl+Shift+F opens the Search view for the whole folder; Ctrl+F searches only the current file.
  2. 2. In the replace box, what does $1 stand for in regex mode?

    $1 inserts what the first capture group matched.
  3. 3. Which toggle shortcut turns regular expressions on in the Find widget?

    Alt+R toggles regex, Alt+C match case and Alt+W whole word.
  4. 4. Why is it wise to commit before using Replace All across files?

    After a big replacement, Source Control shows exactly what changed and lets you discard it.

Summary

  • Ctrl+F and Ctrl+H work in one file; Ctrl+Shift+F and Ctrl+Shift+H work across the folder.
  • Toggle case, whole word and regex with Alt+C, Alt+W, Alt+R.
  • Include and exclude globs narrow Search; search.exclude hides folders such as node_modules.
  • Use $1 for groups, and \u, \U, \l, \L to change case in the replacement.
  • Preview, then replace; commit first so Git can show the diff.

Next: Quick Open and Go To