Previous Page
Next Page

Chapter 8. Forms and Regular Expressions

Regular expressions are an amazingly powerful way to validate and format text strings. Using regular expressions, you can write a line or two of JavaScript code that can accomplish tasks that otherwise would have taken several dozen lines.

A regular expression (often abbreviated as RegExp) is a pattern, written using special symbols, which describes one or more text strings. You use regular expressions to match patterns of text, so that your script can easily recognize and manipulate text. Like an arithmetic expression, you create a regular expression by using operators, in this case operators that work on text, rather than numbers. There are many regular expression operators, and we'll look at some of the most common in this chapter. By learning and using these operators, you'll be able to save yourself a huge amount of effort whenever you need to detect and manipulate text strings.

Regular expressions are also commonly considered to be one of the geekiest parts of programming. You've gotten to the point where you think that you have a good grasp of JavaScript, and then you'll look at a script that contains a regular expression, and it makes no sense at all. If you don't know the syntax, you don't have any way of even guessing what's going on. What does all that gibberish mean?

But the syntax isn't that hard, so long as you break the gibberish down into small, meaningful pieces (at which point it's no longer gibberish). In this chapter, we'll demystify regular expression syntax and discuss how to make your code tighter and more powerful using regular expressions.


Previous Page
Next Page