UTILYARD
guides

How to Find and Replace Text

How find and replace works, when to use regex, case sensitivity, common patterns, and tips for bulk text editing.

What is find and replace?

Find and replace is one of the most fundamental text editing operations: locate every occurrence of a string within a document and substitute it with something else. Every text editor, IDE, spreadsheet application, and word processor has some version of it — usually triggered with Ctrl+H or Cmd+H.

At its simplest, the operation takes two inputs: a search string (what to look for) and a replacement string (what to put in its place). The tool scans through the text, finds every match, and swaps it. You can usually choose to replace one match at a time or all matches in a single pass.

The real power comes when you move beyond literal string matching into regular expressions, which let you describe patterns of text rather than fixed strings. That distinction — literal vs. pattern matching — is the most important concept to understand before using find and replace on anything non-trivial.

Basic vs. regex mode

In basic (literal) mode, your search string is treated as plain text. Every character means exactly itself. Searching for foo.bar finds the exact string "foo.bar" — the dot is just a dot, not a wildcard.

In regex mode, your search string is a regular expression. Characters like ., *, +, and ? have special meaning. A dot matches any character. A + means "one or more of the preceding." This lets you write one pattern that matches many different strings — for example, \d{4}-\d{2}-\d{2} matches any ISO date like 2024-03-15.

Regex mode also unlocks capture groups, which are the feature that makes regex replacement truly powerful. You wrap part of your search pattern in parentheses to "capture" it, then reference it in the replacement string using $1, $2, etc. For example: search (\w+), (\w+) and replace with $2 $1 to flip "Smith, John" into "John Smith".

Use basic mode when you know exactly what string you want to change. Switch to regex mode when you need to match a range of strings, transform structure, or reference parts of the match in the replacement.

Case sensitivity

Most tools offer a case-sensitive and a case-insensitive mode. Case-sensitive matching is the default: searching for Error will not match error or ERROR.

Case-insensitive mode is useful when you don't know or don't care about casing — for instance, finding all references to a brand name that might appear as "utilyard", "UtilYard", or "UTILYARD". In regex, you enable this with the i flag.

One subtlety: case-insensitive replacement does not preserve the original case of each match. The replacement string is inserted verbatim. If you need to replace "color", "Color", and "COLOR" while preserving case in the output, you'll need separate replacements or a scripting approach.

Common use cases

  • Fixing typos across a document — if you've misspelled a name or technical term throughout a long document, a single replace-all corrects every instance at once.
  • Renaming variables or identifiers — change a function name or constant across a file. Use whole-word matching so replacing id doesn't also change identifier or valid.
  • Reformatting data — convert date formats (MM/DD/YYYY to YYYY-MM-DD), swap delimiters in CSV files, or reorder columns using capture groups in regex mode.
  • Stripping unwanted characters — remove trailing spaces, extra blank lines, HTML tags, or log prefixes by replacing a pattern with an empty string.
  • Bulk URL or path updates — update an old domain name or change a folder path across a configuration file in a single operation.

The UtilYard Find and Replace tool handles all of these in the browser — paste your text, define your search and replacement, toggle regex and case sensitivity, and see results instantly.

Useful regex patterns for text replacement

These patterns cover the most common text-cleanup tasks and are compatible with JavaScript, Python, and most editors.

  • Trim trailing whitespace per line
    Search: [ \t]+$ · Replace: (empty)
    Use the multiline flag so $ anchors to each line end.
  • Collapse multiple spaces into one
    Search: + · Replace: (single space)
    Useful after copying text from PDFs or web pages.
  • Flip "Last, First" to "First Last"
    Search: ([A-Za-z]+), ([A-Za-z]+) · Replace: $2 $1
    Capture groups let you reference matched text in the replacement.
  • Convert MM/DD/YYYY to YYYY-MM-DD
    Search: (\d\d)/(\d\d)/(\d{4}) · Replace: $3-$1-$2
    Captures month, day, and year separately, then rearranges them into ISO 8601 format.
  • Strip HTML tags
    Search: <[^>]+> · Replace: (empty)
    A heuristic — handles most well-formed tags but not malformed HTML edge cases.

Tips for safe bulk replacement

  • Preview before committing. Always review what will be changed before hitting Replace All. Confirm the highlighted text is actually what you intended to change.
  • Use word boundaries for short identifiers. When replacing a short term like id or name, use the regex anchor \b so you don't accidentally modify unrelated words that contain those letters.
  • Escape metacharacters in literal searches. If your search string contains ., +, or other regex special characters, either use literal mode or escape them with a backslash in regex mode.
  • Test on a representative excerpt first. Paste a small chunk of text and verify the output looks correct before running the same replacement across thousands of lines.