1

I have the following URL:

http://www.abebooks.com/servlet/BookDetailsPL?bi=1325819827&searchurl=an%3DLofting%252C%2BHugh.%26ds%3D30%26sortby%3D13%26tn%3DDOCTOR%2BDOLITTLE%2527S%2BGARDEN.

Where bi is a identifier for the specific book. How can I extract the book id from the link?

Thanks!

1

6 Answers 6

2

You can to use this regex:

var address = "http://www.abebooks.com/servlet/BookDetailsPL?bi=1325819827&...";
var bi = /[\?&]bi=(\d+)/.exec(address)[1]
alert(bi)
Sign up to request clarification or add additional context in comments.

1 Comment

be careful if bi is not the first param in the url; i.e., it may not always be prefixed with ? but might also be prefixed with &
1
function getBookId()
{
    var query = document.location.split("?")[1];
    var values = query.split("&");
    for(var i = 0; i < values.length; i++)
    {
        a = values[i].split("=");
        if(a[0] === "bi")
            return a[1];
    }
    //some error occurred
    return null;
}

2 Comments

maybe a regex is easier here?
While the idea is sound, I'd suggest using var values = location.search.substring(1).split('&');
0

You can extract the book id (assumed to be only numbers) via a regular expression (and grouping).

var s = "http://www.abebooks.com/servlet/BookDetailsPL?\     
         bi=1325819827&searchurl=an%3DLofting%252C%2BHugh.\
         %26ds%3D30%26sortby%3D13%26tn%3DDOCTOR%2BDOLITTLE\ 
         %2527S%2BGARDEN."

var re = /bi=([0-9]+)&/; // or equally: /bi=(\d+)&/

var match = re.exec(s);

match[1]; // => contains 1325819827

1 Comment

if bi is the last parameter, it won't be followed by an &. Safe bet, I guess, would be /\bbi=(\d+)\b/ - \b matches a word boundary. But I am not sure if something-bi=1235&bi=456 is a valid query string - that's why I went with js.
0

address.split("bi=")[1].split("&")[0]

1 Comment

this might also match something like xyzbi= or otherbi= which would not be the intended effect...
0

Try this

var bookId
var matcher = location.search.match(/(?:[?&]bi=([^&]+))/); // Assuming window.location

if (null !== matcher) {
      bookId = matcher[1];
}

Comments

0

I once had the same problem.
I created a little function to help me out. Don't know where it is but I managed to recreate it:

function get(item,url) {
    if (url == undefined) 
        url = window.location.href;             

    var itemlen = item.length
    var items = url.split('?')[1].split('&');
    for (var i = 0, len = items.length;i<len;i++) {
        if (items[i].substr(0,itemlen) == item)
            return items[i].split('=')[1];  
    }

    return null;
}

So you would use it like:

get('bi');

If the url you gave was your current url, if not you could do:

get('bi','http://www.abebooks.com/servlet/BookDetailsPL?bi=1325819827&searchurl=an%3DLofting%252C%2BHugh.%26ds%3D30%26sortby%3D13%26tn%3DDOCTOR%2BDOLITTLE%2527S%2BGARDEN.')

Hope I didn't leave in any bugs :)

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.