I have a URL with a query string that starts and ends with specific letters. Here is an example and my approach:
Say the address in the address bar "http://localhost:3001/build/?videoUrl=bitcoin.vid.com/money#/"
First I retrieve this url with window.location.href
and save it in a variable x
.
Now I want to check first whether videoUrl
the URL is present or not, and then if it is available, I split the URL and retrieve the required URL, whichbitcoin.vid.com/money
let x = "http://localhost:3001/build/?videoUrl=bitcoin.vid.com/money#/";
let y;
let result;
if(x.indexOf("?videoUrl")>-1) {
y = x.split("?videoUrl=");
result = y[1].split("#")[0];
console.log("Resultant URL:", result);
}
I feel that all the code I wrote is a bit cumbersome. Can someone tell me if there is a more elegant way to do the same?
Note. videoUrl
always unavailable in the url so check if it exists. And please also let me know if I need to do any checks?
.