Regular Expressions for Cleaning Markup
Every web developer finds themselves having to clean up and manipulate tags and Search and Replace can only take you so far. Here are a few I use that save me hours.
Nothing beats BBEdit/Text Wrangler for tasks like this. So save these in the reg-ex library if you use those tools. Coda and Sublime fans can save these as snippets.
- Replace tag attributes. Usage: When you want to strip out the attributes for a certain tag but said tags have different attributes. This is also useful when you need to strip out tags with various attributes.
# Find
<p([^>]*)>
# Replace
<p>
- Repalce open and closing tags
# Find
<(/?)b>
# Replace
<\1strong>
- Match all instances of a tag regardless of each tag's attributes
# Find
\<img(.*)\>