Accessibility

Table of Contents

Introduction to Regular Expressions in Dreamweaver

Using the Not Operator to Match Tag Attributes and Values

Recall that an open bracket followed by a ^ means to match any character not enclosed in the brackets.

When you think about what characters might follow a tag name to form attributes and values, you realize that there are quite a few. For example, the <meta> tag in the example looks like this:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

This line of code contains numbers, alphabetic characters, quotation marks, space characters, equal signs, and semicolons. You have two options when trying to solve the problem of matching all of these possibilities.

Option 1: A Strict Regular Expression

You could create a strict regular expression that accepts all possible regular expression characters within a custom character class, as shown in the following example:

</?[A-Za-z]+[A-Za-z0-9\s-=";/0]*>

This regular expression continues to read as before but now I've inserted a new optional custom character class for the pattern: [A-Za-z0-9\s-=";/0]*>. This means that following the less-than sign and a tag name (<meta), you'll allow for an optional set of characters to appear. The * following this custom class indicates that zero or more instances of the custom class can occur allowing the pattern to match both a tag with attributes and a tag without.

Option 2: A Lazy Regular Expression Using the Not Operator

A simpler approach would be to take advantage of the not operator in order to allow for any and all characters except a greater-than sign, as shown in this example:

</?[A-Za-z]+[^>]*>

Again, you're defining an optional custom character class. In this case, however, the ^ character means "to match any character except for the ones listed in this class"─in this example, a closing greater-than sign.

Option 2 has advantages and disadvantages. Option 1 is useful when it's paramount that the expression be as precise as possible. However, in this case, you might not want to specify every possible character that is acceptable, because attributes can contain other values such as a $ or a # character. Option 2, in this case, might be more flexible.