Regex to find the first image in an image tag in an HTML document

前端 未结 3 380
有刺的猬
有刺的猬 2021-01-25 17:03

What is a regex to find the first image in an image tag in an HTML document? My previous tries have not really worked, as they just matched based on .jpg\" and didn

3条回答
  •  猫巷女王i
    2021-01-25 17:27

    As anubhava correctly points out, regex is not 100% reliable for parsing HTML. However, for one-shot-tasks, (i.e. not production code), a regex solution can do a pretty good job (and is quite fast as well):

    Capture the image URL filename (sans query or fragment) from the first IMG element into group $1:

    ]+?src\s*=\s*['"]?([^\s'"?#>]+)

    Note that there are certainly edge cases where this does not work.

    Edit: Added ">" to the negated SRC attribute value character class.

提交回复
热议问题