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!

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
croft10
  • 11
  • 1

6 Answers6

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)
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 1
    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 `&` – Funka Jan 07 '10 at 04:48
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;
}
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
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
miku
  • 181,842
  • 47
  • 306
  • 310
  • 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. – Amarghosh Jan 06 '10 at 13:27
0

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

  • this might also match something like `xyzbi=` or `otherbi=` which would not be the intended effect... – Funka Jan 07 '10 at 04:50
0

Try this

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

if (null !== matcher) {
      bookId = matcher[1];
}
nickytonline
  • 6,855
  • 6
  • 42
  • 76
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 :)

AntonioCS
  • 8,335
  • 18
  • 63
  • 92