Need JavaScript regular expression to extract Item ID value from eBay URL

egoselfaxis
egoselfaxis used Ask the Experts™
on
I need to find a way to extract the item ID value from an eBay URL using Javascript:

The item ID is boldfaced in the example URL below:

http://cgi.ebay.com/VINTAGE-PRESSED-GLASS-RUBY-FLASHED-DIAMOND-CANDY-DISH_W0QQitemZ120589354693QQcategoryZ4764QQcmdZViewItemQQ_trksidZp3907.m263QQ_trkparmsZalgo%3DSIC%26itu%3DUCI%252BIA%252BUA%252BFICS%252BUFI%252BDDSIC%26otn%3D8%26pmod%3D370178498217%26po%3DLVI%26ps%3D63%26clkid%3D7427724710710527929

Here's what I have so far (see javascript function below), .. but for some reason it's not working.  It's instead just returning the full url instead of just the Item ID value.  

Is there something I'm doing wrong?  

Thanks in advance,
- Yvan


<script language="JavaScript">
      var ebayUrl = "http://cgi.ebay.com/VINTAGE-PRESSED-GLASS-RUBY-FLASHED-DIAMOND-CANDY-DISH_W0QQitemZ120589354693QQcategoryZ4764QQcmdZViewItemQQ_trksidZp3907.m263QQ_trkparmsZalgo%3DSIC%26itu%3DUCI%252BIA%252BUA%252BFICS%252BUFI%252BDDSIC%26otn%3D8%26pmod%3D370178498217%26po%3DLVI%26ps%3D63%26clkid%3D7427724710710527929";            
      itemid = ebayUrl.replace("/QQitemZ(\d+?)QQ/", "$1");            
      alert(itemid);
</script>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
You could do this instead:

var p = ebayUrl.indexOf('QQitemZ');
var item_no = ebayUrl.substr(pos + 7, 12);

var p = ebayUrl.indexOf('QQitemZ');
var item_no = ebayUrl.substr(pos + 7, 12);

Open in new window

HainKurtSr. System Analyst

Commented:
I hate Regex ;)
<script language="JavaScript">
      var ebayUrl = "http://cgi.ebay.com/VINTAGE-PRESSED-GLASS-RUBY-FLASHED-DIAMOND-CANDY-DISH_W0QQitemZ120589354693QQcategoryZ4764QQcmdZViewItemQQ_trksidZp3907.m263QQ_trkparmsZalgo%3DSIC%26itu%3DUCI%252BIA%252BUA%252BFICS%252BUFI%252BDDSIC%26otn%3D8%26pmod%3D370178498217%26po%3DLVI%26ps%3D63%26clkid%3D7427724710710527929";            
      var ix1 = ebayUrl.indexOf("QQitemZ");
      var ix2 = ebayUrl.indexOf("QQ", ix1+6);
      var itemId = ebayUrl.substring(ix1+7,ix2);
      alert(itemId);
</script>

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
See my answer here:  http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_26301233.html#a33127327

since this seems to be a duplicate question.
Sr. System Analyst
Commented:
I managed to get it with Regex ;)


<script language="JavaScript">
      var ebayUrl = "http://cgi.ebay.com/VINTAGE-PRESSED-GLASS-RUBY-FLASHED-DIAMOND-CANDY-DISH_W0QQitemZ120589354693QQcategoryZ4764QQcmdZViewItemQQ_trksidZp3907.m263QQ_trkparmsZalgo%3DSIC%26itu%3DUCI%252BIA%252BUA%252BFICS%252BUFI%252BDDSIC%26otn%3D8%26pmod%3D370178498217%26po%3DLVI%26ps%3D63%26clkid%3D7427724710710527929";            
      var itemId = ebayUrl.match(/QQitemZ(\d+?)QQ/)[1];
      alert(itemId);
</script>

Open in new window

Chandramouli kArchitect
Commented:
you can do with Split function

<script >
var strURL = "http://cgi.ebay.com/VINTAGE-PRESSED-GLASS-RUBY-FLASHED-DIAMOND-CANDY-DISH_W0QQitemZ120589354693QQcategoryZ4764" //Please store URL value to strURL

var aItemID = strURL.split("QQitemZ")[1].split("QQcategory");

alert(aItemID[0]);
</script>

Open in new window

Author

Commented:
Thanks guys!

- yg

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial