using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; public class Test { public static void Main() { Console.WriteLine(ReplaceAll("/]a", new[] { '/', ']' }, 'a')); Console.WriteLine(ReplaceAll("^/]a",new[] { '^', '/', ']' }, '$')); } public static string ReplaceAll(string str, char[] charsToReplace, char replacementCharacter) { if(string.IsNullOrEmpty(str)) { return string.Empty; } var pattern = $"[{string.Concat(charsToReplace).Replace(@"\", @"\\").Replace("-", @"\-").Replace("^", @"\^").Replace("]", @"\]")}]"; return Regex.Replace(str, pattern, replacementCharacter.ToString()); } }