String target = "FOOBar";
target = target.replaceAll("(?i)foo", "");
System.out.println(target);

Output:

Bar

It's worth mentioning that replaceAll treats the first argument as a regex pattern, which can cause unexpected results. To solve this, also use Pattern.quote as suggested in the comments.

Answer from lukastymo on Stack Overflow
🌐
Legend Blogs
legendblogs.com › home › programming › android › case-insensitive replaceall in java : with example
Case-insensitive replaceAll in Java : with example
October 15, 2025 - Normally we replace string by any ... The replaceAll function in the java.lang.String class replaces all substring found in that matches the regular expression to replace....
Discussions

apex - How do I do a case-insensitive replace all for a string? - Salesforce Stack Exchange
I've got a project where I'd like to replicate some of the internal merge field functionality so users can build a link and it'd be really helpful if I could do a case-insensitive string replaceAll... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
Is Java RegEx case-insensitive? - Stack Overflow
In Java, when doing a replaceAll to look for a regex pattern like: CopyreplaceAll("\\?i\\b(\\w+)\\b(\\s+\\1)+\\b", "$1"); (to remove duplicate consecutive case-insensitive words, e.g. Test test), I'm not sure where I put the ?i. I read that it is supposed to be at the beginning, but if I take ... More on stackoverflow.com
🌐 stackoverflow.com
regex - Case Insensitive variable for String replaceAll(,) method Java - Stack Overflow
Can anyone help me with creating a regex for variables in java so that the string variable will be considered to be a case insensitive and replace each and every word like Access, access, etc with WINDOWS of any thing like that? ... Notice that label is a string variable. ... Perhaps also note what replaceAll ... More on stackoverflow.com
🌐 stackoverflow.com
java - regex replace all ignore case - Stack Overflow
You could convert it all to lowercase before doing the search, or look at a regex modifier Pattern.CASE_INSENSITIVE ... private String replaceAllIgnoreCase(final String text, final String search, final String replacement){ if(search.equals(replacement)) return text; final StringBuffer buffer ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jelani Harris
jelaniharris.com › blog › case-insensitive-replaceall-in-java
Case-insensitive replaceAll in Java | Jelani Harris
September 10, 2009 - To create the case sensitive version of replaceAll we do not need to create a new wrapper function or create a utility class somewhere. All we need to do is prepend the Case-insensitve pattern modifier (?i) before our regex to indicate that we don’t care about the case sensitivity of the regex.
🌐
Java String
javastring.net › home › java string replaceall() and replacefirst() methods
Java String replaceAll() and replaceFirst() Methods
July 29, 2019 - Use “(?i)” with the regex string to perform case-insensitive matching. jshell> String str = "Hello WORLD"; str ==> "Hello WORLD" jshell> str.replaceAll("(?i)l", "l"); $29 ==> "Hello WORlD" jshell> str.replaceAll("(?i)L", "l"); $30 ==> "Hello ...
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › how-to-perform-case-insensitive-string-replacement-in-java-using-replaceall-method
How to Perform Case Insensitive String Replacement using replaceAll() in Java - CodingTechRoom
import java.util.regex.*; public ...(?i)hello", "Hi"); System.out.println(result); // Outputs "Hi, World! Hi, Universe!" } } The standard replaceAll() method doesn't alter case by default....
🌐
Qvera
qvera.com › kb › index.php › 1102 › case-insensitive-replaceall
Case Insensitive ReplaceAll - Knowledge Base - Qvera
December 4, 2015 - I have a code snippet that is intended to replace some data in a message that looks like this: var value ... COM', ''); Is there a better way?
🌐
CodingTechRoom
codingtechroom.com › question › java-case-insensitive-substring-replacement
How to Perform Case-Insensitive Substring Replacement in Java - CodingTechRoom
Regular expressions in Java allow for pattern matching with options like case sensitivity. Utilize the replaceAll() method with a regular expression to achieve case insensitivity. Employ the '(?i)' directive in your regex pattern to make the ...
🌐
CodingTechRoom
codingtechroom.com › question › case-insensitive-regex-java
How to Perform Case-Insensitive Regex Matching in Java? - CodingTechRoom
String result = inputString.replaceAll("(?i)\b(\w+)\b(\s+\1)+\b", "$1"); // Case-insensitive duplicate removal · Java supports case-insensitive matching in regex through the use of inline flags. To achieve this, you can use the `(?i)` flag directly in your regex pattern.
🌐
Devgex
devgex.com › en › article › 00047400
Comprehensive Guide to Case-Insensitive String Replacement in Java - DevGex
December 7, 2025 - For case-insensitive string replacement in Java, the core approach involves using replaceAll with the (?i) flag, but for substrings involving special characters, always use Pattern.quote for protection.
🌐
Learn IT University
learn-it-university.com › home › effortless substring replacement in java: a guide to case-insensitive transformations
Effortless Substring Replacement in Java: A Guide to Case-Insensitive Transformations - Learn IT University
July 21, 2024 - As you progress through your coding journey, keep practicing these techniques, and never hesitate to experiment with regex patterns. ... Utilize replaceAll with the (?i) flag for case-insensitive replacements.
🌐
findnerd
findnerd.com › list › view › How-to-replace-Case-Insensitive-chars-in-a-String-in-Java › 6980
How to replace Case-Insensitive chars in a String in Java?
September 17, 2015 - package com.demo; public class TestApps { /* *@param args */ public static void main(String[] args){ String s = "This is to replace Check String check with pattern."; String b = s.replaceAll("(?i:check)", "#@!*"); System.out.println(b); } } output: This is to replace #@!* String #@!* with pattern. in the above example I'm replacing chars "check", it will replace both String "Check" and "check". Hope this will help you :) Tags · Java to replace case-insensitive shars string regex ·
🌐
Bennadel
bennadel.com › blog › 301-case-insensitive-java-regular-expressions-how-did-i-miss-that.htm
Case Insensitive Java Regular Expressions - How Did I Miss That!?!
March 22, 2020 - String::Matches() String::ReplaceFirst() String::ReplaceAll() ... allow for much more powerful AND faster regular expressions. Those methods can handle all the look behinds (negative and possitive) which I don't think ColdFusion can handle at all. That means that they can do much more than things like: ... So, it's speed, but it's also flexability. ... If (?i) and (?i:regex) works, (?-i) might also work, which with some regex libraries turns off case insensitivity for the remainder of the regular expression, e.g.: (?i)libby(?-i)[a-z]