简体   繁体   English

从字符串处理URL

[英]manipulating the url from string

Here is the url where I am getting lost. 这是我迷路的网址。 What is the issue? 有什么问题

I am getting the value as: 我得到的价值是:

mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp?paramsgoes---

But sometimes it comes as: 但是有时候它是这样的:

http://mydomain.mymaindomain.com/asppage.asp?paramsgoes---

So I want to make sure that if the mydomain.mymaindomain.com/ comes extra, I want jquery to try to remove it. 因此,我想确保如果mydomain.mymaindomain.com/变得多余,我希望jquery尝试将其删除。 I am lost as to where I try to do it. 我不知道该在哪里做。

It is not showing http:// but it could be applying or it could not be applying, I'm not sure at this point. 它没有显示http://但它可能正在应用或无法应用,目前我不确定。

One way would be to check whether the last occurrence of http is at the beginning of the string or somewhere in the middle. 一种方法是检查http的最后一次出现是在字符串的开头还是在中间。 There's no need for jQuery, this is vanilla JS: 不需要jQuery,这是普通JS:

str = 'http://somedomain';
var pos = str.lastIndexOf('http');
if (0 != pos) {
    str = str.substring(pos);
}

This will return http://mydomain.mymaindomain.com/asppage.asp? 这将返回http://mydomain.mymaindomain.com/asppage.asp? for: 对于:

http://mydomain.mymaindomain.com/asppage.asp?
http://mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp
mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp

Demo 演示版

Try before buy 购买前尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM