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

请教一个正则转换的问题
请问各位
<script type ="text/javascript">aaa</script> 怎么用正则转换成: <div>aaa</div>

不知道有没有发错版块,若有请版主转移到正确的版块。

------解决方案--------------------
C# code
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);
        }
    }
}

------解决方案--------------------
C# code
            string s = @"<script type =""text/javascript"">aaa</script>";
            string r = Regex.Replace(s, @"(?is)<script.*?>(.*?)</script>", "<div>$1</div>");
            Console.WriteLine(r);
            Console.ReadKey();

------解决方案--------------------
比较帅气的写法。

C# code

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);
        }
    }
}

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

 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>
 */

------解决方案--------------------
探讨
以上的正则我这边都不适用啊,正则也分这么多种的吗?

------解决方案--------------------
HTML code

var html='<script type =\"text/javascript\">aaa</script>' ;

var reg=/<script\b[^>]*?>([^<>]+)</script>/gi;

html=html.replace(reg,'<div>$1</div>');

------解决方案--------------------
探讨

还是不行,我是用在xml里面的!