开发者

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

开发者 https://www.devze.com 2023-01-23 02:26 出处:网络
In my html content, i want to replace all \"bold\" text with \"italic\" existing in between script tag using 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

精彩评论

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