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

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Regular Expression Needed


    I need a regular expression pattern for the following.
    INPUT:
    Code:
    <a class="member"><img src="images/members/somebody-s.jpg" /><span><img src="images/members/somebody.jpg" /></span></a>
    OUTPUT:
    Code:
    <img src="images/members/somebody-s.jpg" />
    The original code shows a small image and if hovered over a large image appears. I am trying to scan a table to extract the img tag for the small image to use in further processing.
    I will use:
    http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    and
    http://www.w3schools.com/jsref/jsref_regexp_exec.asp
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,987
    Rep Power
    9397
    Don't use regular expressions for parsing HTML. Load it up in something that can properly parse HTML (what language are you using?) and grab the <img> tag using whatever mechanisms it gives you.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    I am using Javascript. I know I can use a substring. I know the starting position but I would have to scan for the ending position. Do you have a better method?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,987
    Rep Power
    9397
    Originally Posted by denmarks
    Do you have a better method?
    Yes: load the string up in something that can properly parse HTML and grab the <img> tag using whatever mechanisms it gives you.

    How are you getting the string? AJAX? innerHTML?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Yes: load the string up in something that can properly parse HTML and grab the <img> tag using whatever mechanisms it gives you.

    How are you getting the string? AJAX? innerHTML?
    It is being extracted from a table as I move through it.
    I ended up using:
    Code:
    var rowCell=row_cells[1];
    var theString = rowCell.innerHTML
    cell_content=theString.substring(18,theString.indexOf('<span>'))+"<br />";

IMN logo majestic logo threadwatch logo seochat tools logo