简体   繁体   中英

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. 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.

One way would be to check whether the last occurrence of http is at the beginning of the string or somewhere in the middle. There's no need for jQuery, this is vanilla 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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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