Accessibility

Table of Contents

Introduction to Regular Expressions in Dreamweaver

Background on Regular Expressions

The origins of regular expressions date back to the 1950s when mathematicians began exploring theoretical computer science. This field of research included topics such as automata theory and formal language theory. Stephen Cole Kleene, one of the fathers of theoretical computer science, is credited with inventing regular expressions. Ken Thompson, a major contributor to the development of the UNIX operating system, incorporated regular expressions into the UNIX text editor known as Ed.

Today, support for regular expressions can be found in scripting languages, programming languages, operating systems, and tools. Examples of tools that support regular expressions include Microsoft Internet Explorer, Firefox, Macromedia Flash, Eclipse, and Microsoft Visual Studio .NET. Nearly every modern programming or scripting language offers built-in support for regular expressions or provides a dedicated library as an add-on. Some examples of computer languages that support regular expressions include Macromedia ColdFusion, PHP, ActionScript, JavaScript, Java, C++, C#, Visual Basic .NET, PERL, Ruby, and Python.

Depending on the tool or language, the implementation of regular expressions may vary. Due to the varying levels of support, I recommend that you always find documentation on what is and is not supported by the tool or language that you're using. There are a variety of reasons why the growth in popularity of regular expressions has taken place. For web developers and programmers, the quantity of code in the world has grown exponentially. As such, it's become increasingly difficult to navigate the volume of text that is out there. In addition, although regular expressions ancestry stems from mathematics followed by programming, regular expressions can be used by content developers as well to update large amounts of text easily.

Common Usages

Regular expressions are sometimes referred to as "RegEx" or "RegExp." There are a number of common usages for regular expressions including:

  • Data validation in web forms

    When a user clicks a submit button on a web form, a JavaScript event is triggered that executes code that validates user-entered text that meets a certain requirement. For example, regular expressions can be used to verify that a phone number a user supplied does not contain any alphabetic containers.

  • Processing log files

    Log files are almost always generated in a consistent format so that they can be parsed easily with regular expressions or imported into a database.

  • Updating content

    Regular expressions are used by non-programmers to find or make updates across a large number of files. In the past, I have seen documentation writers leverage regular expressions in order to make hundreds of complex updates by creating a moderately complex regular expression.

  • Extending Dreamweaver

    Though outside the scope of this article, the Dreamweaver Design view is extensible using a mechanism known as translators. A translator uses regular expressions to parse the code of a file and converts it into a tag representation that the Design view can then display. For more information on Translators, see extending Dreamweaver.

  • Cleaning up HTML/CSS code

    Last, but hardly least, regular expressions come in particularly handy when maintaining websites, for example, when removing deprecated code such as the <font> tag or locating multiple instances of a CSS rule. In the exercise later in this article, in "Searching for CSS Inline Styles," I present an example of how to use regular expressions in order to locate inline CSS styles.