How do you do case-insensitive string replacement using regular expressions?

You can use:

myString = Regex.Replace(myString, "/kg", "", RegexOptions.IgnoreCase);

If you’re going to do this a lot of times, you could do:

// You can reuse this object
Regex regex = new Regex("/kg", RegexOptions.IgnoreCase);
myString = regex.Replace(myString, "");

Using (?i:/kg) would make just that bit of a larger regular expression case insensitive – personally I prefer to use RegexOptions to make an option affect the whole pattern.

MSDN has pretty reasonable documentation of .NET regular expressions.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)