提取URL中问号后面的字符串

时间:2018-01-22 17:08:31

标签: javascript

如果有人能帮助我,我会非常感激。

我正在尝试提取最后一个问号后面的字符串(以下示例中为'rrrrrrrrrrrrrrrrrrrrr'):

从:

  

https://www.youtube.com/embed/url=https://sites.google.com/a/yink.net/uok/ee.xml&container=enterprise&view=default&lang=en&country=ALL&sanitize=0&v=9a2d7691ff90daec&libs=core&mid=38&parent=http://j.yink.net/home/sandbox?的 rrrrrrrrrrrrrrrrrrrr

为:

  

rrrrrrrrrrrrrrrrrrrr

我已经尝试过这段代码,但它没有给出正确的结果:

  

document.getElementById(“demo”)。innerHTML = window.location.search.slice(1);

2 个答案:

答案 0 :(得分:0)

使用lastIndexOf获取“?”的最后一个索引然后使用substr获取子字符串。

确保您清理字符串并确保存在“?”在它首先,否则它将引发错误。

let str = `https://www.youtube.com/embed/url=https://sites.google.com/a/yink.net/uok/ee.xml&container=enterprise&view=default&lang=en&country=ALL&sanitize=0&v=9a2d7691ff90daec&libs=core&mid=38&parent=http://j.yink.net/home/sandbox?rrrrrrrrrrrrrrrrrrrr`;

console.log(str.substr(str.lastIndexOf("?")+1));

答案 1 :(得分:0)

var url = window.location.href会为您提供网址

然后你想要在?使用

之后抓住所有内容

var queryString = url ? url.split('?')[1] : window.location.search.slice(1);

我将答案拼凑在一起:https://www.sitepoint.com/get-url-parameters-with-javascript/

编辑: ------------------------------------- < / p>

要获取最后一个问号后面的文字,您可以使用:

url.split('?').pop()代替url.split('?')[1]

(感谢: @I曾经摔跤了一只曾经提到的熊