tags:

views:

51

answers:

1

In my html content, i want to replace all "bold" text with "italic" existing in between script tag using c#.

I have two option here for applying regular expression a) replace all between script tag b) replace all before the ending of script tag

So what will be the regular expression using any method?

A: 

Something like this (untested!):

String pattern = Regex.Escape(@"<script>") + @"(?<inner_text>.*@)" + Regex.Escape(@"</script>");

Regex rx = new Regex(pattern);

foreach (Match m in rx.Matches(input))
{
    string captured = m.Groups["inner_text"];//maybe a .Value is missing?!
}
//OR:
rx.Replace(input,MyMatchEvaluator);

//...
string MyMatchEvaluator(Match m)
{
     return @"<script>" + MyTransformingFunction(m.Groups["inner_text"]) + @"</script>";
}

UPDATE: I got the non-greedy flag wrong. somehow I thougt it was '@', but in fact it is '?'. The fixed pattern:

String pattern = Regex.Escape(@"<script>") + @"(?<inner_text>.*?)" + Regex.Escape(@"</script>");

You could replace the '*' with a '+' to only match non-empty script tags.

UPDATE #2: the '@' was in my head because of the VisualStudio regex "Find" - it's the non-greedy version of '*' for VisualStudio's "Find in Files"

Johannes Colmsee