简体   繁体   English

使用JavaScript删除部分网址

[英]Remove part of url using javascript

I need to remove part of url if it matches //videoid?v= 如果与//videoid?v=相匹配,我需要删除网址的一部分

assuming my url can be 假设我的网址可以是

 - www.abc.com/video/
 - www.abc.com/video/videoid?v=1234567
 - www.abc.com/video//videoid?v=1234567

in case url has // forward slash before videoid?v= then i need to remove single / from the url so that url will be correct such as www.abc.com/video//videoid?v=1234567 如果url在videoid?v=之前有//正斜杠,那么我需要从url中删除单个/ ,以便url正确,例如www.abc.com/video//videoid?v=1234567

currentURL = document.URL;

You can use regex and use it like this to remove double occurrences of "/": 您可以使用regex并像这样使用它来消除重复出现的“ /”:

"www.abc.com/video//videoid?v=1234567".replace(/([^:]\/)\/+/g, "$1");

Working example: https://jsfiddle.net/os5yypqm/3/ 工作示例: https : //jsfiddle.net/os5yypqm/3/

EDIT: 编辑:

I edited the JSFiddle to include "http://" in front of the url so that you can see that this does not affect it (and also read it here). 我编辑了JSFiddle,在URL前面包含“ http://”,以便您可以看到这不会影响它(并在此处阅读)。 Can't use it on SO though so you need to see the fiddle. 但是不能在SO上使用它,因此您需要查看小提琴。

If your only concern is the double slash. 如果您唯一关心的是双斜线。

var currentUrl = 'www.abc.com/video//videoid?v=1234567';
currentUrl.split('//').join('/');

Results in www.abc.com/video/videoid?v=12345678 www.abc.com/video/videoid?v=12345678结果

如果后跟正斜杠和"v"用空字符串替换正斜杠

"www.abc.com/video//videoid?v=1234567".replace(/\/(?=\/v)/g, "")

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

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