Find and Replace
Replace text using literal strings or regular expression patterns with capture group support.
Back to all tools on ToolForge
Find
Replace with
Input Text
Result
About Find and Replace
This find and replace tool performs text substitution using either literal string matching or regular expression patterns. It supports global replacement, capture groups, and pattern-based transformations for text editing and data processing workflows.
Literal vs Regex Mode Comparison
| Feature | Literal Mode | Regex Mode |
|---|---|---|
| Matching | Exact string match | Pattern-based matching |
| Special Characters | Treated literally | Have special meaning |
| Wildcards | Not supported | .*, \\d, \\w, [A-Z], etc. |
| Capture Groups | Not supported | Yes ($1, $2, etc.) |
| Character Classes | Not supported | [A-Z], [0-9], \\s, etc. |
| Anchors | Not supported | ^ (start), $ (end) |
| Quantifiers | Not supported | +, *, ?, {n,m} |
| Best For | Simple text replacement | Pattern matching, data transformation |
Regex Pattern Reference
| Pattern | Description | Example Match |
|---|---|---|
. |
Any character except newline | a, 1, space |
\\d |
Digit character | 0-9 |
\\w |
Word character | a-z, A-Z, 0-9, _ |
\\s |
Whitespace character | space, tab, newline |
[abc] |
Character class (a, b, or c) | a, b, or c |
[^abc] |
Negated class (not a, b, or c) | any except a, b, c |
^pattern |
Start of string/line | pattern at beginning |
pattern$ |
End of string/line | pattern at end |
\\bword\\b |
Word boundary | whole word "word" |
Quantifier Reference
| Quantifier | Description | Example |
|---|---|---|
* |
0 or more (greedy) | a* matches "", "a", "aaa" |
+ |
1 or more (greedy) | \\d+ matches "1", "123" |
? |
0 or 1 (optional) | colou?r matches "color" or "colour" |
{n} |
Exactly n times | \\d{4} matches "2024" |
{n,m} |
Between n and m times | \\d{2,4} matches "24", "2024" |
*? |
0 or more (non-greedy) | <.*?> matches "<tag>" minimally |
Replacement Algorithm
JavaScript Find and Replace Implementation:
// Literal mode: uses split/join for exact string replacement
function literalReplace(text, find, replacement) {
return text.split(find).join(replacement);
}
// Regex mode: uses RegExp with capture group support
function regexReplace(text, pattern, replacement) {
const regex = new RegExp(pattern, "g");
return text.replace(regex, replacement);
}
// Count matches
function countMatches(text, pattern, isRegex) {
const searchPattern = isRegex
? new RegExp(pattern, "g")
: new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g");
const matches = text.match(searchPattern);
return matches ? matches.length : 0;
}
// Usage:
const text = "I have 123 apples and 456 oranges";
// Literal replacement
literalReplace(text, "apples", "oranges");
// "I have 123 oranges and 456 oranges"
// Regex replacement - remove all numbers
regexReplace(text, "\\d+", "");
// "I have apples and oranges"
// Regex with capture groups - swap numbers
regexReplace("Doe, John", "(\\w+), (\\w+)", "$2 $1");
// "John Doe"
Capture Group Examples
Example 1: Swap first and last name
Find: (\w+), (\w+)
Replace: $2 $1
Input: "Doe, John"
Output: "John Doe"
Example 2: Convert date format MM/DD/YYYY → YYYY-MM-DD
Find: (\d{2})/(\d{2})/(\d{4})
Replace: $3-$1-$2
Input: "12/25/2024"
Output: "2024-12-25"
Example 3: Wrap numbers in brackets
Find: (\d+)
Replace: [$1]
Input: "I have 42 dollars and 99 cents"
Output: "I have [42] dollars and [99] cents"
Example 4: Add quotes around words
Find: (\w+)
Replace: "$1"
Input: "hello world"
Output: ""hello" "world""
Example 5: Extract email domain
Find: \w+@(\w+\.\w+)
Replace: $1
Input: "Contact: [email protected]"
Output: "Contact: example.com"
Common Use Cases
- Data Cleanup: Remove unwanted characters, normalize whitespace, strip HTML tags
- Code Refactoring: Rename variables, update function calls, transform imports
- Content Editing: Fix repeated typos, update terminology, reformat dates
- Log Processing: Extract or mask sensitive data (IPs, emails, IDs)
- Format Conversion: Transform CSV to JSON, reformat dates, normalize phone numbers
- Text Transformation: Case changes, word reordering, field extraction
- SEO: Update internal links, modify meta descriptions, fix heading structures
Special Characters Reference
These characters have special meaning in regex mode and must be escaped with backslash to match literally:
| Character | Regex Meaning | Escape to Match |
|---|---|---|
. |
Any character | \. |
* |
0 or more | \* |
+ |
1 or more | \+ |
? |
0 or 1 | \? |
[ ] |
Character class | \[ \] |
( ) |
Capture group | \( \) |
{ } |
Quantifier | \{ \} |
^ |
Start anchor | \^ |
$ |
End anchor | \$ |
| |
Alternation (OR) | \| |
\ |
Escape character | \\ |
Replacement Examples
Example 1: Remove all numbers
Find: \d+
Replace: (empty)
Input: "I have 123 apples and 456 oranges"
Output: "I have apples and oranges"
Example 2: Normalize whitespace
Find: \s+
Replace: " "
Input: "too many spaces here"
Output: "too many spaces here"
Example 3: Mask email addresses
Find: \w+@\w+\.\w+
Replace: [EMAIL REDACTED]
Input: "Contact [email protected] or [email protected]"
Output: "Contact [EMAIL REDACTED] or [EMAIL REDACTED]"
Example 4: Convert camelCase to snake_case
Find: ([a-z])([A-Z])
Replace: $1_$2
Input: "userName and firstName"
Output: "user_name and first_name"
Example 5: Extract phone numbers (US format)
Find: (\d{3})[-.]?(\d{3})[-.]?(\d{4})
Replace: ($1) $2-$3
Input: "Call 555-123-4567 or 555.987.6543"
Output: "Call (555) 123-4567 or (555) 987-6543"
How to Use Find and Replace
- Enter find text: Type the literal text or regex pattern to search for.
- Enter replacement: Type the substitution text (use $1, $2 for capture groups).
- Enable regex (optional): Check the box if using regex patterns.
- Paste input text: Enter the text to process.
- Click Replace All: All occurrences are replaced globally in the output.
- Copy result: Use the output in your documents or code.
Tips
- Test regex patterns on small text samples first
- Use capture groups for complex rearrangements
- Empty replacement deletes all matches
- Escape special characters in literal mode if they have regex meaning
- Use character classes for case-insensitive single characters: [Hh]ello
Frequently Asked Questions
- How does find and replace work?
- Find and replace scans text for occurrences of a search string or pattern and substitutes them with a replacement value. In literal mode, it uses exact string matching (split/join). In regex mode, it uses the RegExp engine with pattern matching and capture group substitution via $1, $2, etc.
- What is the difference between literal and regex mode?
- Literal mode treats the find string as plain text—special characters like ., *, + have no special meaning. Regex mode interprets the find string as a regular expression pattern, enabling wildcards (.*), character classes ([A-Z]), quantifiers (\d+), and anchors (^, $). Use regex mode for pattern-based matching, literal mode for exact text replacement.
- How do capture groups work in replacements?
- Capture groups are defined by parentheses in the regex pattern. Each group is numbered left-to-right starting from 1. In the replacement string, use $1, $2, etc. to reference captured text. Example: Find '(\w+), (\w+)' with Replace '$2 $1' transforms 'Doe, John' to 'John Doe'.
- Why do special characters need escaping?
- In regex mode, characters like ., *, +, ?, [, ], (, ), ^, $, |, \ have special meanings. To match them literally, escape with backslash: \. matches a literal dot, \* matches an asterisk. In literal mode, these characters are treated as-is without escaping.
- Is the replacement case-sensitive?
- Yes, matching is case-sensitive by default. 'hello' does not match 'Hello' or 'HELLO'. For case-insensitive regex matching, use the (?i) flag at the pattern start or character classes like [Hh]ello. Literal mode has no built-in case-insensitive option.
- What happens with empty replacement text?
- An empty replacement string deletes all matched text. This is useful for removing unwanted patterns: use \s+ to collapse multiple spaces, \d+ to remove all numbers, or [^\w\s] to strip punctuation. The matches are replaced with nothing, effectively deleting them.