Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Location
    washington, dc
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regex search and replace: adding missing alt attributes across site

    I'm trying to use regex to search all img tags that are missing alt attributes in my entire site. I'm using the following to do so:

    <img\s+((width|height|border|class|id|src|usemap|hspace|vspace)="[^"]+"\s*)+/?>

    That's working fine, but I also want to add generic alt attributes (i.e. alt="") to all img elements that don't have them (partly in order to validate, partly to add a placeholder for some non-skilled help I'll be getting). This is where I'm stuck. I'm very new to regular expressions and can't figure out how, if at all, regex can handle replaces. The text editors I have at my disposal are <oxygen /> XML Editor and Notepad ++.

    Am I barking up the wrong tree with regex? Alternatives? Anyone know of any tools, preferably freeware, that might help?

    The site I'm working on is very large, so I'd really like to find some automated option.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by drett View Post
    I'm trying to use regex to search all img tags that are missing alt attributes in my entire site. I'm using the following to do so:

    <img\s+((width|height|border|class|id|src|usemap|hspace|vspace)="[^"]+"\s*)+/?>

    That's working fine, but I also want to add generic alt attributes (i.e. alt="") to all img elements that don't have them (partly in order to validate, partly to add a placeholder for some non-skilled help I'll be getting). This is where I'm stuck. I'm very new to regular expressions and can't figure out how, if at all, regex can handle replaces. The text editors I have at my disposal are <oxygen /> XML Editor and Notepad ++.

    Am I barking up the wrong tree with regex? Alternatives? Anyone know of any tools, preferably freeware, that might help?

    The site I'm working on is very large, so I'd really like to find some automated option.
    I assume that is a *nix system and your files are html ( the file extension), so you can use the 'toolbox' tools, find and sed like that:

    Code:
    find -name "*.html" -exec sed -i 's/<img\([^>]\+\)>/<img\1 alt=""\/>/g' {} \;
    best regards


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •