Getting specific value from url query string

javascript, regex, url

Solution

I came up with this:

var final_id;
var url = document.URL;
var id_check = /[?&]id=([^&]+)/i;
var match = id_check.exec(url);
if (match != null) {
    final_id = match[1];
} else {
    final_id = "";
}

Works for:

https://www.blabla.com/ebookedit?id=B0077RQGX4&commit=Go
final_id = 'B0077RQGX4'

https://www.blabla.com/ebookedit?SomethingElse=Something&id=B0077RQGX4&commit=Go
final_id = 'B0077RQGX4'

https://www.blabla.com/ebookedit?commit=go&id=B0077RQGX4
final_id = 'B0077RQGX4'

https://www.blabla.com/ebookedit?commit=Go
final_id = ''

https://www.blabla.com/ebookedit?id=1234&Something=1&id=B0077RQGX4&commit=Go
final_id = '1234'

Problem

I have the following URL, and I want to get the "id" value from it using JavaScript. ``` https://www.blabla.com/ebookedit?id=B0077RQGX4&commit=Go ``` I started with this code: ``` var url = document.URL; var id_check = /^\id...\E; // NOT SURE HERE var final_id = new RegExp(id_check,url); ``` I want to extract the id "B0077RQGX4" and save it into a variable that I would later modify. How would I do it and which functions would I use in JavaScript?

Original source