Javascript:选择性地从网址中删除哈希(或散列),以使网址保持有效或可用 [英] Javascript: Selectively remove hash (or hashes) from URLs, so that the URL remains valid or usable

查看:44
本文介绍了Javascript:选择性地从网址中删除哈希(或散列),以使网址保持有效或可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我们有以下网址:

1. http://example.com#hash0
2. http://example.com#hash0#hash1
3. http://example.com#hash0/sample.net/
4. http://example.com#hash0/sample.net/#hash1
5. http://example.com#hash0/image.jpg
6. http://example.com#hash0/image.jpg#hash1
7. something.php#?type=abc&id=123
8. something.php#?type=abc&id=123#hash0
9. something.php/?type=abc&id=#123
....................................

还有更多这样的排列,您明白了.如何从这类URL中有选择地删除无关的"散列而不会影响这些URL的功能(以使它们保留为<完整链接或图像)?

and more permutations of this kind, you got the point. How can I selectively remove the "irrelevant" hashes from this kind of URLs without affecting the functionality of those URLs (so that they remain complete links or images)?

例如,从此列表中的数字1希望删除#hash0,从#hash0和#hash1删除2,从3删除,因为它后面是路径的延续(是的,有可能,请检查此处),从4中仅删除#hash1,从5中删除保留它,但从6中仅删除#hash1,...,从9中删除,因为它可能与查询相关(尽管不确定),依此类推.基本上,我只想删除在它们之后没有任何可用值(例如路径,查询,图像文件等)的哈希-无关"哈希,例如#top,#bottom等,它们是指当前页面.

For example, from number 1 in this list I would like #hash0 to be removed, from 2 both #hash0 and #hash1, from 3 I'd like to keep it, since it's followed by a continuation of the path (yes, it's possible, check here), from 4 remove only #hash1, from 5 keep it, but from 6 remove just #hash1, ... , and from 9 I think keep it, since it might have relevance to the query (not sure about it though), and so on. Basically I'd like to remove only the hashes that don't have anything usable (like paths, queries, image files, etc.) after them - "irrelevant" hashes like #top, #bottom and such, that are referring to the current page.

我正在做的事情还涉及从相对的URL中获取绝对URL(借助于新的锚的href或新的URL对象的href),因此提供了一种解决方案(例如

I'm working on something that also involves getting the absolute URLs from relative ones (with the help of either a new anchor's href or new URL object's href), so a solution (like here) that can "blend in" with the location object's properties (like .protocol, .host, .pathname, .search, .hash, etc.) is preferable - since it might be more "trustworthy" since it's built in, but a good (and shorter) regex would be acceptable as well. All in all, shorter solutions are preferable, as I don't want my project to do extra unnecessary work for every link or image link that it encounters while it parses the entire current URL.

推荐答案

也许这就是您想要的,带有正则表达式.

Maybe this what you want, with a regular expression.

var urls = [
        'http://example.com#hash0',                   // remove
        'http://example.com#hash0#hash1',             // remove
        'http://example.com#hash0/sample.net/',       // keep
        'http://example.com#hash0/sample.net/#hash1', // remove #hash1
        'http://example.com#hash0/image.jpg',         // keep
        'http://example.com#hash0/image.jpg#hash1',   // remove #hash1
        'something.php#?type=abc&id=123',             // keep
        'something.php#?type=abc&id=123#hash0',       // remove #hash0
        'something.php/?type=abc&id=#123',            // remove #123
    ],
    result = urls.map(h => h.replace(/(?:#[^#\/\?\.]*)*#[^#\/\?\.]*$/gi, ''));
    
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于Javascript:选择性地从网址中删除哈希(或散列),以使网址保持有效或可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆