0

I would like to use a regular expression to determine the video ID of NHL.com videos.

Example urls are the following:

1. http://video.nhl.com/videocenter/console?id=789500&catid=35
2. http://video.senators.nhl.com/videocenter/console?id=790130&catid=1141
3. http://video.nhl.com/videocenter/?id=2013020884-605-h

From these examples, the values I would need are as follows:

1. 789500
2. 790130
3. 2013020884-605-h

I would like to use the match() function to obtain the ID following ?id=, the ID's can include characters that are alphanumeric, underscore, and dash.

JimmyBanks
  • 4,178
  • 8
  • 45
  • 72

1 Answers1

4

You can use:

/\?id=([^&]+)/gi

i.e.

var re = /[?&]id=([^&#]+)/i;

And use matched group #1:

var m = str.match(re);
var id = m[1];

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Global matches via `.match()` [don't include groups in JavaScript](http://stackoverflow.com/questions/19913667/javascript-regex-global-match-groups). In addition, the regex should probably be changed to match a question mark _or an ampersands_ at the beginning. In addition, leave out any hash tags at the end in case of a fragment identifier in the URL. For instance: `/[?&]id=([^]+)/i` – Pluto Mar 25 '15 at 19:24