mhe*_*ers 35 javascript jquery expression query-string
我正在尝试使用youtube数据api生成视频播放列表.但是,视频网址需要youtube.com/watch?v=3sZOD3xKL0Y格式,但api生成的是youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata.所以我需要做的是能够选择&符号之后的所有内容并将其从url中删除.使用javascript和某种正则表达式的任何方式吗?
gol*_*cks 46
我错过了什么?
为什么不:
url.split('?')[0]
Run Code Online (Sandbox Code Playgroud)
Jac*_*kin 36
简单:
var new_url = old_url.substring(0, old_url.indexOf('?'));
Run Code Online (Sandbox Code Playgroud)
修改:这将从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);
}
Run Code Online (Sandbox Code Playgroud)
hri*_*iya 23
嗯......寻找更好的方式......在这里
var onlyUrl = window.location.href.replace(window.location.search,'');
Run Code Online (Sandbox Code Playgroud)
use*_*716 20
示例: http ://jsfiddle.net/SjrqF/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.slice( 0, url.indexOf('&') );
Run Code Online (Sandbox Code Playgroud)
要么:
示例: http ://jsfiddle.net/SjrqF/1/
var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';
url = url.split( '&' )[0];
Run Code Online (Sandbox Code Playgroud)
使用这个功能:
var getCleanUrl = function(url) {
return url.replace(/#.*$/, '').replace(/\?.*$/, '');
};
// get rid of hash and params
console.log(getCleanUrl('https://sidanmor.com/?firstname=idan&lastname=mor'));Run Code Online (Sandbox Code Playgroud)
如果您想要所有 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.orgRun Code Online (Sandbox Code Playgroud)
//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;
}
Run Code Online (Sandbox Code Playgroud)