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.

  1. 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>
  1. Repalce open and closing tags
# Find
<(/?)b>

# Replace
<\1strong>
  1. Match all instances of a tag regardless of each tag's attributes
# Find
\<img(.*)\>