我现在有一个字符串,里面东西内容很多,
“<link href="/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="/images/style_top.css" rel="stylesheet" type="text/css" />”
像这种href里地址是相对地址的,我现在要替换成绝对地址,变成为“<link href="http://www.baidu.com/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="http://www.baidu.com/images/style_top.css" rel="stylesheet" type="text/css" />”帮忙写下替换语句,谢谢各位大大了。参考:
(?<=href\s*=)(?:[ \s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)[^""']*(?:[ \s>""'])
这个正则自己试试,应该可以使用,我没用好。
“<link href="/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="/images/style_top.css" rel="stylesheet" type="text/css" />”
像这种href里地址是相对地址的,我现在要替换成绝对地址,变成为“<link href="http://www.baidu.com/images/style_0908.css" rel="stylesheet" type="text/css" />
<link href="http://www.baidu.com/images/style_top.css" rel="stylesheet" type="text/css" />”帮忙写下替换语句,谢谢各位大大了。参考:
(?<=href\s*=)(?:[ \s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)[^""']*(?:[ \s>""'])
这个正则自己试试,应该可以使用,我没用好。
解决方案 »
- ie8兼容问题
- ASP,点击面页按钮,弹出系统选择文件路径对话框
- ASP.Net输出复杂表格得问题!高手指教!
- 谁给提供点iis总停止服务方面的解决办法或者资料?
- 在DataGrid添加编辑后怎样让它在最右边??
- win2000 server iis配置 站点.net环境,求解决,先道谢了!
- 小弟正在配置.Text博客,遇到一个问题请教大虾.
- 使用过webchart的高手请进!(也许是一个很简单的问题!)
- asp.net和asp有什么区别?
- 一个已编译在线运行的项目,如何新增一个页面(无源码无法再次编译)使其可以正常运行sql语句?
- 如何在网页中的局部打开word文档
- dataset 数据赋到数组中???
不过像这种规律这么明显的,用程序Replace也可以做到吧
Console.Write(Regex.Replace(str,@"href=""([^>]*)""+([^>]*)",@"href=""http://www.baidu.com/$1"""));
str = Regex.Replace(str, @"<link\s+href=""([^""]+)""\s+rel=""stylesheet""\s+type=""text/css""\s*/>", "<link href=\"http://www.baidu.com$1\" rel=\"stylesheet\" type=\"text/css\" />");
Console.WriteLine(str);
str = Regex.Replace(str, @"<link\s+href=""([^""]+)""\s+rel=""stylesheet""\s+type=""text/css""\s*/>", "<link href=\"http://www.baidu.com$1\" rel=\"stylesheet\" type=\"text/css\" />");
Console.WriteLine(str);
string strContent = "<link href=\"/images/style_0908.css\" rel=\"stylesheet\" type=\"text/css\" />" +
"<link href=\"/images/style_top.css\" rel=\"stylesheet\" type=\"text/css\" />";
Regex re = new Regex( "(?<=<link href=\")[^/]*", RegexOptions.Multiline | RegexOptions.Compiled);
strContent = re.Replace( strContent,"http://www.baidu.com");
Console.Write(Regex.Replace(str, "<link[^>].*?href=\"([^>]*)\"+([^>]*)", "<link href=\"http://www.baidu.com/$1\""));
string s = " <link href='/images/style_0908.css' rel='stylesheet' type='text/css' /> <link href='/images/style_top.css' rel='stylesheet' type='text/css' />";
s.Replace("/images", "http://www.baidu.com/images");
string result = reg.Replace(yourStr, @"http://www.baidu.com$0");