Search code examples
javascriptregexcoldfusionimdb

Given a string, how to determine if it's an IMDB id?


For the regex experts out there, I need to determine whether a given string is a properly formatted IMDB Id.

Need to do this in either ColdFusion or JavaScript. ColdFusion would look like: REFind(regex, "tt32423"), and JavaScript would look like regex.test('tt32432'), if we can get the value of regex.


Solution

  • Based on the discussion tab of the page you linked to, you can use this RegEx in JavaScript: /ev\d{7}\/\d{4}(-\d)?|(ch|co|ev|nm|tt)\d{7}/

    example:

    /ev\d{7}\/\d{4}(-\d)?|(ch|co|ev|nm|tt)\d{7}/.test("tt2964642")
    

    Even though the ID you provided in your example fails this test, when entered as a route parameter to http://imdb.com/title/tt32423 in my browser, i'm redirected to http://imdb.com/title/tt0032423. Given that, depending on how you'll use this, you could adjust the regex to included 1 to 7 digits instead

    {7} becomes {1,7}

    Up to you.