Case insensitive replace
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Case insensitive string replacement is a common operation in text processing. The standard replace function in most languages is case sensitive by default, meaning replacing "cat" will not match "Cat" or "CAT". This article shows how to perform case insensitive replacements in Python, JavaScript, Java, C#, and Go, along with edge cases you need to handle.
The Core Problem
Consider this scenario: you have user-generated content and need to replace all variations of a word regardless of capitalization. Searching for "error" should also match "Error", "ERROR", and "eRRoR". A simple str.replace("error", "warning") will miss all of those variants.
The solution depends on the language. Most approaches use either regex with a case-insensitive flag or manual case normalization.
Python: Using re.sub
Python's re.sub function with the re.IGNORECASE flag is the cleanest approach:
If the search term contains special regex characters, escape them first:
The re.escape call is critical. Without it, characters like (, ), ., and * in the search term would be interpreted as regex metacharacters.
JavaScript: Regex with the i Flag
JavaScript's String.replace accepts a regex with the i (case insensitive) and g (global) flags:
For dynamic search terms where you cannot use a regex literal, construct a RegExp object:
Java: Pattern.CASE_INSENSITIVE
Java does not have a built-in case insensitive replace on String, but Pattern with CASE_INSENSITIVE handles it:
Pattern.quote wraps the search string so that any special regex characters are treated as literals.
C#: Regex.Replace with RegexOptions
C# provides Regex.Replace with RegexOptions.IgnoreCase:
For frequent replacements, compile the regex to avoid repeated pattern parsing:
Go: strings.NewReplacer Does Not Support Case Insensitivity
Go's standard library does not have a built-in case insensitive replace. Use the regexp package:
The (?i) prefix enables case insensitive matching in Go's regex syntax.
Preserving Original Case
Sometimes you want to replace a word but keep the original capitalization pattern. For example, replacing "cat" with "dog" should turn "Cat" into "Dog" and "CAT" into "DOG":
Common Pitfalls
Not escaping regex metacharacters. If your search term contains characters like ., (, ), *, or +, they will be interpreted as regex patterns instead of literal characters. Always use re.escape (Python), Pattern.quote (Java), Regex.Escape (C#), or a manual escape function (JavaScript, Go).
Unicode case folding. Simple lowercasing does not handle all Unicode cases correctly. For example, the German sharp s (double-s) uppercases to "SS". If you need full Unicode case folding, use re.UNICODE in Python or Pattern.UNICODE_CASE in Java.
Performance with large texts. Compiled regex is significantly faster than creating a new pattern on each call. If you are performing the same replacement across thousands of strings, compile the pattern once and reuse it.
Replacing partial words unintentionally. Replacing "is" case insensitively in "This island is isolated" will produce "Th warning land warning warning olated" if you are not careful. Use word boundary anchors (\b) to match whole words only.
Summary
Case insensitive replacement requires regex with a case-insensitive flag in most languages. Always escape the search term to prevent regex metacharacter issues. Use re.IGNORECASE in Python, the /gi flags in JavaScript, Pattern.CASE_INSENSITIVE in Java, RegexOptions.IgnoreCase in C#, and (?i) in Go. For repeated replacements, compile the regex pattern once. When matching whole words only, add word boundary anchors to avoid partial matches.

