日期:2014-05-18  浏览次数:20975 次

C# 正则表达式替换HTML 中的 script 标签
要用C# 给每一个script 标签中的引用地址加上时间戳如何替换?

例如: <script src="../../Jscript/jquery-1.6.4.min.js" language="javascript" type="text/javascript"></script>
替换为:<script src="../../Jscript/jquery-1.6.4.min.js?tt==dd" language="javascript" type="text/javascript"></script>

------解决方案--------------------
C# code

string tempStr = @"<script src=""../../Jscript/jquery-1.6.4.min.js"" language=""javascript"" type=""text/javascript""></script>";
                string pattern = @"(?i)(?<=<script[^>]*?src=(['""])[^'""]*)(?=\1[^>]*?>)";
                string result = Regex.Replace(tempStr, pattern, "?tt=dd");

------解决方案--------------------
没写完整
C# code

string input=@"<script src=""../../Jscript/jquery-1.6.4.min.js"" language=""javascript"" type=""text/javascript""></script>";
Regex.Replace(input,@"(?i)js(?="")",@"$0?tt==dd");

------解决方案--------------------
string html = @"<script src=""../../Jscript/jquery-1.6.4.min.js"" language=""javascript"" type=""text/javascript""></script>";
Regex reg = new Regex(@"(?i)<script (?:[^>]*?)src=[""'][^""']+");
html = reg.Replace(html, "$0?tt=" + DateTime.Now.Ticks);
Console.WriteLine(html);

===输出
<script src="../../Jscript/jquery-1.6.4.min.js?tt=634726852429582117" language="javascript" type="text/javascript"></script>