简体   繁体   English

正则表达式替换之前的文本 </script> 标签或C#中脚本标签中的文本之间

[英]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#. 在我的html内容中,我想使用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 我在这里有两个用于应用正则表达式的选项:a)在脚本标签之间替换所有内容b)在脚本标签结尾之前全部替换

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" 更新#2:因为VisualStudio正则表达式“查找”,“ @”在我脑海中-它是VisualStudio“查找文件”的“ *”的非贪婪版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM