如何将HTML格式字符串中的相对URL都替换成绝对URL?例,当前目录为
http://community.csdn.net/Expert/HTML字符串为...
<link href="../css/style.css">
...
<a href="PostNew.asp">发新帖</a>
<img src="Image1.gif">
...替换后的为
...
<link href="http://community.csdn.net/Expert/../css/style.css">
...
<a href="http://community.csdn.net/Expert/PostNew.asp">发新帖</a>
<img src="http://community.csdn.net/Expert/Image1.gif">
...如果用正则表达式如何处理?
请给出具体的关键的代码。或者谁能给个方法,方法不限。多谢!
http://community.csdn.net/Expert/HTML字符串为...
<link href="../css/style.css">
...
<a href="PostNew.asp">发新帖</a>
<img src="Image1.gif">
...替换后的为
...
<link href="http://community.csdn.net/Expert/../css/style.css">
...
<a href="http://community.csdn.net/Expert/PostNew.asp">发新帖</a>
<img src="http://community.csdn.net/Expert/Image1.gif">
...如果用正则表达式如何处理?
请给出具体的关键的代码。或者谁能给个方法,方法不限。多谢!
解决方案 »
- 请问下,该怎么把自己的GRIDVIEW输出成EXCEL```还要有统计行```
- .net点击图片放大
- vs2005安装,超变态的一个问题!
- vs2003做好了asp网页,该如何发布?
- Linq查询,返回一个值,类似ExecuteScalar
- 各位老大,上机试试吧,要不都是帖出代码,我拿着却无法实现(不排除我是大笨蛋的可能)。在线等。
- 100分求个图片上传完整的源码(不够再加)!
- 关于外部函数调用的问题。谢谢大家,恨急
- System.Net.WebException: 在 WebClient 请求期间发生异常,路径拒绝访问
- 应用程序稳定性的问题?
- 关于文件下载的问题,,大家帮忙顶啊~~~~
- 在asp中写入中文值的cookies,在asp.net中读出该cookies值却是乱码?请问高手如何解决?
///
那就把所有可能为Url的格式找出来,用正则或者直接String替换参考:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<a href="PostNew.asp">发新帖</a>
</form>
</body>
</HTML>///
//在后置代码里面
///
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public class Filter : Stream
{
private Stream _stream;
private long _position;
StringBuilder _output = new StringBuilder(); public Filter( Stream stream )
{
_stream = stream;
} public override bool CanRead
{
get { return true; }
} public override bool CanSeek
{
get { return true; }
} public override bool CanWrite
{
get { return true; }
} public override long Length
{
get { return 0; }
} public override long Position
{
get { return _position; }
set { _position = value; }
} public override long Seek( long offset, System.IO.SeekOrigin direction )
{
return _stream.Seek(offset, direction);
} public override void SetLength( long length )
{
_stream.SetLength( length );
} public override void Close()
{
_stream.Close();
} public override void Flush()
{
_stream.Flush();
} public override int Read( byte[] buffer, int offset, int count )
{
return _stream.Read( buffer, offset, count );
} public override void Write( byte[] buffer, int offset, int count )
{
string [] rows = System.Text.UTF8Encoding.UTF8.GetString( buffer, offset, count ).Split( Environment.NewLine.ToCharArray() );
StringBuilder output = new StringBuilder();
foreach( string row in rows )
{
if ( row.Trim() != string.Empty )
{
string pattern = "<a.*href=\"([^\"]*)\"[^/]*>";
Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
string newRow = regex.Replace( row.Trim(), "<a href=\"http://" + System.Web.HttpContext.Current.Request.Url.Host + System.Web.HttpContext.Current.Request.ApplicationPath + "/" +"$1\">", -1 );
output.Append( newRow.Trim() );
output.Append( Environment.NewLine );
}
}
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes( output.ToString() );
_stream.Write( data, 0, data.Length );
} } public class WebForm1 : System.Web.UI.Page
{
protected override void OnPreRender(EventArgs e)
{
Response.ContentType = "text/html;charset=utf-8";
Response.Filter = new Filter( Response.Filter );
Response.StatusCode = 200;
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
}
#endregion
}
}
<img src="img.gif">
<param src=''> // 单引号
<link href=style.css> // 无引号
<script src=""></script>
可能还有漏掉的,再者我还想把正文中的某一关键字进行替换
即替换在标签外的满足条件的文本,例:将关键字 a 替换成 A,当然实际情况是替换成更有意义的<b>a</b>替换前:
<a href="boy.htm">I am a boy.</a>
替换后:
<a href="boy.htm">I Am A boy.</a>
而不是
<A href="boy.htm">I Am A boy.</A>这是两个问题,如能给出替换关键字的正则表达式(换绝对URL的问题我不急,可以先不考虑),我将不盛感激!
<param.*src=\'([^\"]*)\'[^/]*>
<link.*href=([^\"]*)[^/]*>
<script.*src=\"([^\"]*)\"[^/]*></script>2、
string pattern = "<a.*href=\"([^\"]*)\"[^/]*>(\\w*)</a>";
Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
string newRow = string.Empty;
if ( regex.IsMatch( row.Trim() ) )
{
newRow = "<a href=" + regex.Match( row.Trim() ).Groups[1].Value + ">" + regex.Match( row.Trim() ).Groups[2].Value.Replace( "a", "A" ) + "</a>";
}
我对
"<a.*href=\"([^\"]*)\"[^/]*>(\\w*)</a>"
这个正则表达式很有兴趣。但实际情况可能复杂些,我还想不出如何去改这个正则表达式。我们现在来重新整理一下我的问题。一个部分一个部分来做,让问题简单化。我现在最想得到的是可以实现下面功能的一个方法:有一个很长的 string 类型字符串 html,内容是一个完整的 HTML 文档,以<html>开始,以</html>结束,其中有很复杂的标签,如<img>,<a></a>,<td></td>等等。现在不考虑 URL 的替换,我只想把 html 里面的所有标签外的关键字(不区分大小写)
string keyword
进行替换,替换为
"<b>" + keyword + "</b>"例:string keyword = "a";//原文
string html = @"
...
<body>
<img src='abc.gif'>
<br>
<a href='iamaprogrammer.aspx'>I am a programmer!</a>
</body>
...
";替换后的文本为
"
...
<body>
<img src='abc.gif'>
<br>
<a href='iamaprogrammer.aspx'>I <b>a</b>m <b>a</b> progr<b>a</b>mmer!</a>
</body>
...
"
而不是
"
...
<body>
<img src='<b>a</b>bc.gif'>
<br>
<<b>a</b> href='i<b>a</b>m<b>a</b>progr<b>a</b>mmer.<b>a</b>spx'>I <b>a</b>m <b>a</b> progr<b>a</b>mmer!</<b>a</b>>
</body>
...
"我现在就想知道这个替换关键字的正则表达式如何写,如有时间还请指点一二。
谢谢!!