Regex for escaping

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baylor

    Regex for escaping

    My C# code writes out javascript to the page and to do that certain
    characters need to be escaped. So if the javascript string shows "Customer:
    Bob's Diner" i have to change Bob's to Bob\\'s

    i tried writing a regex replace to do this but apparently don't know how.
    What i tried was:

    string charactersToEsc ape = @"'\\"; //--- apostrophe and \ for \n
    string result = Regex.Replace(t ext, "[" + charactersToEsc ape + "]", "\\$1");

    The $1 syntax obviously isn't right. i tried a named group like <x> but
    apparently don't know the syntax quite right for that either

    -baylor

Working...