简体   繁体   English

使用javascript删除网址的某些部分

[英]Remove some part of URL using javascript

I have a URL which will be something like below 我有一个网址,如下所示

http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg

I want URL which will be something like this 我想要这样的网址

NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg

Here is what I get in variable 这是我得到的变量

VSATSaving.PANAROMIC_120 = document.getElementById('ImgPanaromic120').src;

how to get that using javascript. 如何使用javascript来获取。 Tried with lastIndexOf but it is not working 尝试使用lastIndexOf但无法正常工作

You can create a new URL object and use the pathname property to extract the data. 您可以创建一个新的URL对象,并使用pathname属性提取数据。

 const myUrl = new URL(document.getElementById('ImgPanaromic120').src); console.log(myUrl.pathname); 
 <img id="ImgPanaromic120" src="http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg"/> 

 var a = document.createElement('a'); a.href = 'http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg'; console.log(a.pathname); 

window.location.pathname将为您提供路径。

You can simply use window.location.pathname since this is a url. 您可以简单地使用window.location.pathname因为这是一个URL。

For other strings, you can use indexOf , split , substring and many of the other string functions. 对于其他字符串,可以使用indexOfsplitsubstring和许多其他字符串函数。

 //example using `split` var str = "http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg"; var paths = str.split("http://localhost:22306/") console.log(paths[1]); 

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

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