#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    1
    Rep Power
    0

    Smile Related to regular expression


    I would like to know regular expression(s):

    1. to find all the occurences of <img> tags (in HTML text)
    that are enclosed within <a> tags like <a><img /></a>

    2. to find all the occurences of <img> tags (in HTML text)
    that are not enclosed within <a> tags
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,122
    Rep Power
    4258
    This would depend on which scripting language you are using. However, many of them use PCRE, including PHP, so I will show you how I would do it in PHP.

    1)
    Code:
    "/<a[^>]*>.*(<img[^>]*>)<\/a>/isU"
    2)
    Code:
    "/<[^a][a-z]+[^>]*>.*(<img[^>]*>)<\/[^a][a-z]+>/isU"
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo