如何将HTML格式字符串中的相对URL都替换成绝对URL?

neodotnet 2005-03-06 09:51:47
如何将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">
...

如果用正则表达式如何处理?
请给出具体的关键的代码。

或者谁能给个方法,方法不限。

多谢!
...全文
236 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
neodotnet 2005-03-11
  • 打赏
  • 举报
回复
Up!!!!!!!!!!!!
neodotnet 2005-03-10
  • 打赏
  • 举报
回复
首先再次向无语大哥表示感谢。
我对
"<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>
...
"

我现在就想知道这个替换关键字的正则表达式如何写,如有时间还请指点一二。
谢谢!!
gOODiDEA 2005-03-09
  • 打赏
  • 举报
回复
参考:

1、

<img.*src=\"([^\"]*)\"[^/]*>
<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>";
}


neodotnet 2005-03-08
  • 打赏
  • 举报
回复
无语大哥,非常感谢你的源码,不过我最感兴趣的部分是下面的正则表达式

"<a.*href=\"([^\"]*)\"[^/]*>"

但实际还有下面的情况
<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的问题我不急,可以先不考虑),我将不盛感激!



coldpanth 2005-03-07
  • 打赏
  • 举报
回复
帮你顶,偶也学学
gOODiDEA 2005-03-07
  • 打赏
  • 举报
回复
在当前页面的OnPreRender事件里把所有的Url找出来替换成你需要的形式
gOODiDEA 2005-03-07
  • 打赏
  • 举报
回复
关键是你如何替换所有的啊,一个HTML格式字符串里有好多,你如何替换全部的,又不是简单的一个。
///
那就把所有可能为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
}
}
neodotnet 2005-03-07
  • 打赏
  • 举报
回复
关键是你如何替换所有的啊,一个HTML格式字符串里有好多,你如何替换全部的,又不是简单的一个。
dnboy 2005-03-07
  • 打赏
  • 举报
回复
path=Request.PhysicalApplicationPath+当前路径可不可以啊?
neodotnet 2005-03-07
  • 打赏
  • 举报
回复
关键是如何找出来如何替换啊?
haoduotnt 2005-03-07
  • 打赏
  • 举报
回复
我也来一个`学习`
yeaky 2005-03-06
  • 打赏
  • 举报
回复
Server.UrlDecode()

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧