我正在尝试使用youtube数据api生成视频播放列表。但是,视频网址需要使用youtube.com/watch?v=3sZOD3xKL0Y格式,但api生成的是youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata。所以我需要做的是能够选择&符号之后的所有内容并将其从url中删除。使用javascript和某种正则表达式的任何方法吗?
答案 0 :(得分:41)
我错过了什么?
为什么不:
url.split('?')[0]
答案 1 :(得分:35)
简单:
var new_url = old_url.substring(0, old_url.indexOf('?'));
修改: 这将从url中删除所有参数或片段
var oldURL = [YOUR_URL_TO_REMOVE_PARAMS]
var index = 0;
var newURL = oldURL;
index = oldURL.indexOf('?');
if(index == -1){
index = oldURL.indexOf('#');
}
if(index != -1){
newURL = oldURL.substring(0, index);
}
答案 2 :(得分:21)
嗯......寻找更好的方法......这里是
var onlyUrl = window.location.href.replace(window.location.search,'');
答案 3 :(得分:20)
示例: http://jsfiddle.net/SjrqF/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.slice( 0, url.indexOf('&') );
或:
示例: http://jsfiddle.net/SjrqF/1/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.split( '&' )[0];
答案 4 :(得分:5)
//user113716 code is working but i altered as below. it will work if your URL contain "?" mark or not
//replace URL in browser
if(window.location.href.indexOf("?") > -1) {
var newUrl = refineUrl();
window.history.pushState("object or string", "Title", "/"+newUrl );
}
function refineUrl()
{
//get full url
var url = window.location.href;
//get url after/
var value = url = url.slice( 0, url.indexOf('?') );
//get the part after before ?
value = value.replace('@System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]','');
return value;
}
答案 5 :(得分:2)
使用此功能:
var getCleanUrl = function(url) {
return url.replace(/#.*$/, '').replace(/\?.*$/, '');
};
// get rid of hash and params
console.log(getCleanUrl('https://sidanmor.com/?firstname=idan&lastname=mor'));
如果您想要所有href部件,请使用:
var url = document.createElement('a');
url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';
console.log(url.href); // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
console.log(url.protocol); // https:
console.log(url.host); // developer.mozilla.org
console.log(url.hostname); // developer.mozilla.org
console.log(url.port); // (blank - https assumes port 443)
console.log(url.pathname); // /en-US/search
console.log(url.search); // ?q=URL
console.log(url.hash); // #search-results-close-container
console.log(url.origin); // https://developer.mozilla.org
答案 6 :(得分:1)
您可以使用RegEx来匹配v
的值并自行构建网址,因为您知道网址是youtube.com/watch?v = ...
var url = 'http://youtube.com/watch?v=3sZOD3xKL0Y';
alert(url.match(/v\=([a-z0-9]+)/i));
答案 7 :(得分:1)
好吧,我正在使用它:
stripUrl(urlToStrip){
let stripped = urlToStrip.split('?')[0];
stripped = stripped.split('&')[0];
stripped = stripped.split('#')[0];
return stripped;
}
或:
stripUrl(urlToStrip){
return urlToStrip.split('?')[0].split('&')[0].split('#')[0];
}
答案 8 :(得分:0)
例如,我们有:
example.com/list/search?q=Somethink
您需要使用 window.location.href 这样的变量url:
example.com/list/edit
来自网址:
example.com/list/search?q=Somethink
example.com/list/
var url = (window.location.href);
url = url.split('/search')[0];
url = (url + '/edit');
这是简单的解决方案:-)
答案 9 :(得分:0)
这对我有用:
window.location.replace(window.location.pathname)
答案 10 :(得分:0)
没有 splits.. :) 正确/万无一失的方法是让原生浏览器 BUILT-IN functions 使用 urlParams
完成繁重的工作,繁重的工作已为您完成.
//summary answer - this one line will correctly replace in all current browsers
window.history.replaceState({}, '', `${location.pathname}?${params}`);
// 1 Get your URL
let url = new URL('https://tykt.org?unicorn=1&printer=2&scanner=3');
console.log("URL: "+ url.toString());
// 2 get your params
let params = new URLSearchParams(url.search);
console.log("querys: " + params.toString());
// 3 Delete the printer param, Query string is now gone
params.delete('printer');
console.log("Printer Removed: " + params.toString());
// BELOW = Add it back to the URL, DONE!
___________
现在将所有内容整合到您的实时浏览器中
// Above is a breakdown of how to get your params
// 4 then you simply replace those in your current browser!!
window.history.replaceState({}, '', `${location.pathname}?${params}`);