...

View Full Version : regex search and replace: adding missing alt attributes across site



drett
10-01-2009, 12:21 AM
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.

oesxyl
10-01-2009, 11:57 PM
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:



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


best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum