简体   繁体   中英

Remove part of url using javascript

I need to remove part of url if it matches //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

currentURL = document.URL;

You can use regex and use it like this to remove double occurrences of "/":

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

Working example: 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). Can't use it on SO though so you need to see the fiddle.

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

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

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

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