开发者

Regular expression to replace text before </script> tag or between text in script tag in c#

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) r开发者_如何学Pythoneplace all before the ending of script tag

So what will be the regular expression using any method?


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜