JavaScript is useful for a lot more than opening pop-ups. If you use HTML forms on your website, and want to make sure that your visitors submit valid data on those forms, you might want to consider using some regular expressions in JavaScript. Alejandro Gervasio explains how, with many excellent examples.
Regular expressions in JavaScript - The Basics (Page 2 of 9 )
At times, regular expressions can look fairly complex, but when it comes right down to it, they are actually text string themselves. As we can see, in the following example, we are coding a regular expression that searches for the text �JavaScript� (with no quotes):
JavaScript
Pretty simple, isn�t it? Any string containing the text �JavaScript� matches this regular expression. In fact, this is more than an equal comparison, because it�s matching a string somewhere within another string. It can be anywhere, unless specified otherwise.
But it's not always so simple. As real needs arise, finding more complex string patterns can be a lot more difficult. Special characters might be needed, among other things. So, let�s begin working our way through a few examples to explain the basic regular expression syntax.
Anchoring
It�s very useful to represent that a string must start with a specific character or many of them, and end with another. That�s known as string anchoring. For anchoring strings, a caret (^) may be used to indicate the beginning of a string. A dollar sign ($) is used to indicate the end.
For example:
JavaScript // Matches �JavaScript is great�, �The power of JavaScript� and �What is JavaScript?�
Using anchoring characters:
^JavaScript // Matches �JavaScript shines�, but not �I love JavaScript�
JavaScript$ // Matches �I like JavaScript�, but certainly not �JavaScript is powerful�
^JavaScript$ // matches only �JavaScript� and nothing else.