David542
David542

Reputation: 110462

Match an IMDb url in javascript

I am trying to match an IMDb url, but I keep getting the following error:

/(^http://imdb\.com/title/tt(\d)+/\.+season=(\d)+(.+)?$)
|(^http://imdb\.com/title/tt(\d)+/(.+)?$)
/.test('http://www.imdb.com/title/tt0429046/?ref_=fn_al_tt_1')
Uncaught SyntaxError: Unexpected token ILLEGAL

What is this error and what should the correct input be?

Another option I could do is the broader:

/imdb.com\/title\/tt(\d)+/(.+)?$/.test('http://www.imdb.com/title/tt0429046/?ref_=fn_al_tt_1')

However, for this one I get Uncaught SyntaxError: Unexpected token .

Upvotes: 0

Views: 254

Answers (3)

user557597
user557597

Reputation:

@smithy is correct answer.

Here they are escaped.

First one /(^http:\/\/imdb\.com\/title\/tt(\d)+\/\.+season=(\d)+(.+)?$)|(^http:\/\/imdb\.com\/title\/tt(\d)+\/(.+)?$)/

     (
          ^ http://imdb\.com/title/tt
          ( \d )+
          /
          \.+ season =
          ( \d )+
          ( .+ )?
          $ 
     )
  |  (
          ^ http://imdb\.com/title/tt
          ( \d )+
          /
          ( .+ )?
          $ 
     )

Second one /imdb\.com\/title\/tt(\d)+\/(.+)?$/

 imdb \. com/title/tt
 ( \d )+
 /
 ( .+ )?
 $ 

Upvotes: 0

Weafs.py
Weafs.py

Reputation: 23002

Here is a working RegEx:

/(^http:\/\/www\.imdb\.com\/title\/tt(\d)+\/\.+season=(\d)+(.+)?$)|(^http:\/\/www\.imdb\.com\/title\/tt(\d)+\/(.+)?$)/

Demo

Upvotes: 0

smathy
smathy

Reputation: 27971

You need to escape the /s within your regex, ie: http:\/\/ ...etc.

Upvotes: 3

Related Questions