Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Regular Expression for Image Source

3.00/5 (2 votes)
28 Sep 2010CPOL1 min read 15.8K  
A Regular Expression to get the source value of an image tag regardless of the location of the src attribute

Here is a Regular Expression to get the source value of an image tag regardless of the location of the src attribute.

(?<=img\s+[^>]*src\=[\x27\x22])[^\x27\x22]*(?=[\x27\x22])

Options: case insensitive

  • Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=img\s+[^>]*src\=[\x27\x22])»
    • Match the characters “img” literally «img»
    • Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s+»
      • Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    • Match any character that is NOT a “>«[^>]*»
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • Match the characters “src” literally «src»
    • Match the character “=” literally «\=»
    • Match a single character present in the list below «[\x27\x22]»
    • ASCII character 0×27 (39 decimal) «\x27»
    • ASCII character 0×22 (34 decimal) «\x22»
  • Match a single character NOT present in the list below «[^\x27\x22]*»
    • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • ASCII character 0×27 (39 decimal) «\x27»
    • ASCII character 0×22 (34 decimal) «\x22»
  • Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[\x27\x22])»
    • Match a single character present in the list below «[\x27\x22]»
      • ASCII character 0×27 (39 decimal) «\x27»
      • ASCII character 0×22 (34 decimal) «\x22»

Created with RegexBuddy

View my technical blog entries on .

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)