Home > Blockchain >  Match first occurance of URL with certain subdomain
Match first occurance of URL with certain subdomain

Time:12-08

I receive by API a long string in HTML format. For example:

<b>Hello</b> and welcome to the show. We will be presenting Dune <img class=\"banner\" src=\"https://img.imdb.com/title/tt1160419/mediaviewer/rm2910452737/" alt border=\"0\"></ br> Related : https://imdb.com/best-of/2021-top-10-stars/ls554084528/mediaviewer/rm1882384641/ <img class=\"banner\" src=\"https://img.imdb.com/title/tt1160419/mediaviewer/rm2914563987/" alt border=\"0\">

I would like the regex to match only the first URL that has .img subdomain so only "https://img.imdb.com/title/tt1160419/mediaviewer/rm29145632987/"

so far I have this:

https:\/\/img\.imdb\.com\/[^"]*

but it matches both URLs

Will be used in some javascript code FWIW

CodePudding user response:

See regex in use here :

https:\/\/(?:.*?)(?=\")

Demo

It's necessary to delete at the end of the regular expression /g to have the good functioning.

I think also that your regex works fine if you don't use global flags /g at the end.

Demo

  • Related