<anchor title="下载">
<go href="down/hello.rar" />下载
</anchor>
替换为
<a titile="下载" href="down/hello.rar" >下载</a>str.replace(.....)
<go href="down/hello.rar" />下载
</anchor>
替换为
<a titile="下载" href="down/hello.rar" >下载</a>str.replace(.....)
<anchor title="下载">
<go href="down/hello.rar" />下载
</anchor>
替换为
<a tile="下载" href="down/hello.rar" >下载</a>
string str = @"<anchor title=""下载"">
<go href=""down/hello.rar"" />下载
</anchor>"; Regex reg = new Regex(@"(?is)<anchor(?<part1>[^>]*)>(?<part2>.*?)<go.*?(?<part4>href\s*=\s*\S+).*?/>(?<part3>.*?)</anchor>");
foreach (Match m in reg.Matches(str))
{
Console.WriteLine("<a" + m.Groups["part1"] +" "+ m.Groups["part4"] + ">" + m.Groups["part2"] + m.Groups["part3"] + "</a>");
}
string str = @"<anchor title=""下载"">
<go href=""down/hello.rar"" />下载
</anchor>"; Regex reg = new Regex(@"(?is)<anchor(?<part1>[^>]*)>(?<part2>.*?)<go.*?(?<part4>href\s*=\s*\S+).*?/>(?<part3>.*?)</anchor>");
Console.WriteLine(reg.Replace(str, "<a${part1} ${part4}>${part2}${part3}</a>"));
要用Replace的话 就改成这样
我想这样替换strData =strData.replace(/anchor/g,"a") ,但是 > <go 这一部份取不了,唉
var str='<anchor title="下载"><go href="down/hello.rar" />下载</anchor>';
var reg=/<anchor([^>]*)>(.*?)<go.*?(href\s*=\s*\S+).*?\/>(.*?)<\/anchor>/i;
var newStr = str.replace(reg,'<a$1 $3>$2$4</a>');
alert(newStr);
</script>这个呢?
我现在用最笨的办法,能解决,就是太笨了啊,就是把不要的取掉
下面是我的方法
//strData =strData.replace(/anchor/g,"a");
//strData =strData.replace(/下载">/g,'"');
//strData =strData.replace(/<go/g,"");
<script>
var str = '<anchor title="下载">\r\n';
str += '<go href="down/hello.rar" />下载1\r\n';
str += '</anchor>\r\n';
str += '23333\r\n';
str += '<anchor title="下载">\r\n';
str += '<go href="1111111.rar" />下载2\r\n';
str += '</anchor>\r\n';
str += '122222222222222';
var reg = /<anchor([\s\S]*?)>([\s\S]*?)<go[\s\S]*?(href\s*=\s*\S+)[\s\S]*?\/>([\s\S]*?)<\/anchor>/gi;
var newStr = str.replace(reg, '<a$1 $3>$2$4</a>');
alert(newStr);
</script>
这个应该没啥问题了吧,只不过结果保留的换行而已