修复 JS 中的内部链接

发布于 2024-09-03 15:51:43 字数 433 浏览 9 评论 0原文

我刚刚创建了一个脚本,通过服务器端 JS 从网页中提取文章。 (如果您感兴趣:它用于 http://pipes.yahoo.com/fb55/expandr .)

我刚刚在内部链接方面遇到了一点问题。有些页面包含如下链接:

/subfolder/subpage.html

我需要做的是修复它们并设置根目录,如下所示:

protocol://secondlevel.firstlevel/subfolder/subpage.html

我正在使用 E4X 来处理页面。我不想展示我目前令人毛骨悚然的尝试,它有问题而且速度很慢。有人能为我提供解决方案吗?

I just created a script which extracts the article out of a webpage via server-side JS. (If your interested: it's used for http://pipes.yahoo.com/fb55/expandr .)

I just got a little problem with internal links. Some pages include links like:

/subfolder/subpage.html

What I would need to do is fixing them and setting there root, like this:

protocol://secondlevel.firstlevel/subfolder/subpage.html

I'm using E4X for processing the page. I don't want to show my current creepy try, it's buggy and slow. Does anybody have a solution for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2024-09-10 15:51:43

您可以使用一些正则表达式重写它们:

var baseUrl = "http://somesite.com/somepage"
var root = baseUrl.match(/^[^:]+:\/\/[^\/]+\//)[0];
// "http://somesite.com/"

var HTML = "<a href='/testing'>test</a> and <a class='test' href=\"/foo/bar\"> </a>";

HTML.replace(/<a [^>]*href=["']\/([^'"]+)["']/ig, function (whole, url) {
  return whole.replace("/"+url, root+url);
});

// "<a href='http://somesite.com/testing'>test</a> and <a class='test' href=\"http://somesite.com/foo/bar\"> </a>"

You may be able to rewrite them with some Regular Expression:

var baseUrl = "http://somesite.com/somepage"
var root = baseUrl.match(/^[^:]+:\/\/[^\/]+\//)[0];
// "http://somesite.com/"

var HTML = "<a href='/testing'>test</a> and <a class='test' href=\"/foo/bar\"> </a>";

HTML.replace(/<a [^>]*href=["']\/([^'"]+)["']/ig, function (whole, url) {
  return whole.replace("/"+url, root+url);
});

// "<a href='http://somesite.com/testing'>test</a> and <a class='test' href=\"http://somesite.com/foo/bar\"> </a>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文