0

If anyone could help me with me this, it would very appreciative.

I'm trying to extract the string that comes after the last question mark ('rrrrrrrrrrrrrrrrrrrr' in the following example):

from:

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

to:

rrrrrrrrrrrrrrrrrrrr

I've tried this code, but it doesn't give the right result:

document.getElementById("demo").innerHTML = window.location.search.slice(1);

Eli
  • 91
  • 2
  • 7

2 Answers2

2

var url = window.location.href will give you the URL

Then you'll want to grab everything after the ? using

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

I pieced the answer together from: https://www.sitepoint.com/get-url-parameters-with-javascript/

Edit: -------------------------------------

To get the text after the last question mark then you can use:

url.split('?').pop() instead of url.split('?')[1]

(Credit to: @I wrestled a bear once )

Badrush
  • 1,247
  • 1
  • 17
  • 35
  • Thanks Badrush! I need to extract the text only from the *last* question mark. I have several question marks in the URL... – Eli Jan 22 '18 at 17:16
  • 1
    @Eli - if you want to use the split method you could use `url.split('?').pop()` instead of `url.split('?')[1]` and you'll get the last one. – I wrestled a bear once. Jan 22 '18 at 17:22
0

Use lastIndexOf to get the last index of the "?" and then use substr to get the substring.

Make sure you sanitize the string and make sure there is a "?" in it first or else it will throw an error.

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));
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116