Regular Expression to extract src attribute from img tag

后端 未结 7 1527
刺人心
刺人心 2020-12-14 19:46

I am trying to write a pattern for extracting the path for files found in img tags in HTML.

String string = \"

        
7条回答
  •  旧时难觅i
    2020-12-14 20:26

    This one only grabs the src only if it's inside of an tag and not when it is written anywhere else as plain text. It also checks if you've added other attributes before or after the src attribute.

    Also, it determines whether you're using single (') or double (") quotes.

    \
    

    So for PHP you would do:

    preg_match("/\/", $string, $matches);
    echo "$matches[1]";
    

    for JavaScript you would do:

    var match = text.match(/\/)
    alert(match[1]);
    

    Hopefully that helps.

提交回复
热议问题