I have a URL with a query string which starts and ends with a specific letters. Here's an example and my approach:
Let's say the url in the address bar is "http://localhost:3001/build/?videoUrl=bitcoin.vid.com/money#/"
I first extract this url using the window.location.href
and save it in a variable x
.
Now, I want to first check if the videoUrl
is present in the URL or not and then if it is available, I split the URL and extract the required URL which is bitcoin.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 the whole code which I wrote is a bit cumbersome. Could anyone let me know if there is more elegant way to do the same?
Note: videoUrl
is not available always in the URL, hence the check if it exists. And please also let me know if I need to further make any checks?