日期:2014-05-18 浏览次数:20756 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string source = "<script type =\"text/javascript\">aaa</script>"; string result = "<div>" + Regex.Match(source, @"\<script.*?\>(\w+)\</script\>").Groups[1].Value + "</div>"; Console.WriteLine(result); } } }
------解决方案--------------------
string s = @"<script type =""text/javascript"">aaa</script>"; string r = Regex.Replace(s, @"(?is)<script.*?>(.*?)</script>", "<div>$1</div>"); Console.WriteLine(r); Console.ReadKey();
------解决方案--------------------
比较帅气的写法。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string source = "<script type =\"text/javascript\">aaa</script>"; string result = Regex.Replace(source, @"<(script)[^>]*>((?:(?!</\1>).)*)</\1>","<div>$2</div>"; Console.WriteLine(result); } } }
------解决方案--------------------
string source = @"<script type =\"text/javascript\">aaa</script> <script type =\"text/javascript\">bbb</script> "; string result = Regex.Replace(source, @"(?i)<(script)[^>]+>(.*?)</\1>","<div>$2</div>"; Console.WriteLine(result); /* <div>aaa</div> <div>bbb</div> */
------解决方案--------------------
var html='<script type =\"text/javascript\">aaa</script>' ; var reg=/<script\b[^>]*?>([^<>]+)</script>/gi; html=html.replace(reg,'<div>$1</div>');
------解决方案--------------------