2

I'm trying to retrieve a certain number from a string. But i can't figure out how to isolate the part i need.

The string:

https://intra.site.com/departments/travel/Lists/Booking/fd_Item_Display.aspx?List=8af14ed7-3bde-4ec0-b62a-9516324c967e&ID=15&Source=https%3A%2F%2Fintra%2Emonjasa%2Ecom%2Fdepartments%2Ftravel%2FPages%2Fdefault%2Easpx&ContentTypeId=0x0100B7DC1AFF519B6343BC8014EB1910DFAB

I need the number after ID=.

I did try to use string.replace() without luck.

How could I do this with regex?

0

3 Answers 3

3

Check this out :

function getParameterByName(url, parameter) {
   var regex = new RegExp("[\\?&]" + parameter + "=([^&#]*)"),
   results = regex.exec(url);
   return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var url = 'https://intra.site.com/departments/travel/Lists/Booking/fd_Item_Display.aspx?List=8af14ed7-3bde-4ec0-b62a-9516324c967e&ID=15&Source=https%3A%2F%2Fintra%2Emonjasa%2Ecom%2Fdepartments%2Ftravel%2FPages%2Fdefault%2Easpx&ContentTypeId=0x0100B7DC1AFF519B6343BC8014EB1910DFAB';
var parameter = 'ID';

console.log( getParameterByName(url, parameter) );
// Log => 15
Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

var str = 'https://intra.site.com/departments/travel/Lists/Booking/fd_Item_Display.aspx?List=8af14ed7-3bde-4ec0-b62a-9516324c967e&ID=15&Source=https%3A%2F%2Fintra%2Emonjasa%2Ecom%2Fdepartments%2Ftravel%2FPages%2Fdefault%2Easpx&ContentTypeId=0x0100B7DC1AFF519B6343BC8014EB1910DFAB';
var id = (str.match(/&ID=([^&]*)/i) || ['', ''])[1];
//=> 15

1 Comment

Thx m8 - this works perfectly. I will mark as correct answer in 10 min.
1

Try this. Easier to understand.

    

    function getID()
    {
      
    var str = "https://intra.site.com/departments/travel/Lists/Booking/fd_Item_Display.aspx?List=8af14ed7-3bde-4ec0-b62a-9516324c967e&ID=15&Source=https%3A%2F%2Fintra%2Emonjasa%2Ecom%2Fdepartments%2Ftravel%2FPages%2Fdefault%2Easpx&ContentTypeId=0x0100B7DC1AFF519B6343BC8014EB1910DFAB";

    var id = str.match(/ID=(\d*)/)[1];        
     
    alert(id);
      
    }
<input type="button" onclick="getID()" value="Click me">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.