JavaScript RegExp Object
JavaScript's String objects include shortcut like the indexOf() method to determine the presence of absence of a predetermined string inside another. The limitation of this approach lies precisely in this need for predetermination. Fortunately, JavaScript's regular expression bring you much more power by allowing you to check for and extract patterns, rather than exact matches.
Regular expressions exist at the core most modern programming languages, and vary only superficially between implementations. By learning JavaScript regular expressions, you will have learned more than half or Perl's flavor of regular expressions, on which JavaScript's are based. These tutorials attempt at making it easy and intuitive to learn these powerful pattern matching techniques.
Searching for Text vs. Pattern Matching
The JavaScript String object offers the case-sensitive indexOf() and lastIndexOf() methods to find pre-defined strings of text. We can work around the case-sensitivity of by using the toLowerCase() or toUpperCase() methods. This approach remains limited, since it requires an advance knowledge of the terms to search or replace. This is hardly a realistic scenario: think of free text input fields in web forms.
Example: Validating Email Addresses
Using patterns, on the other end, offers you more lattitude in predictability. Although each email address is different, they all share a certain pattern. Worded in plain English, the pattern would correspond to "a set of non-spaced characters, not including the '@' symbol; then, an '@' symbol; then, another series of non-spaced characters."
Using the indexOf() method would limit you to checking the presence of the "@" symbol, and perhaps checking for common domain extensions (.com, .net. org, etc.) - which means that your script would have to be updated anytime a new extension is widely used.
JavaScript RegExp Properties
Our tutorial will cover the properties available to JavaScript regular expressions objects (or "RegExp objects").
- The global property determines if matching should stop after the first match.
- The ignoreCase property renders regular expressions case-insensitive.
JavaScript RegExp Methods
While JavaScript's RegExp object has methods of its own, you may often find yourself using String methods in conjunction with regular expressions. Our regular expression tutorials therefore include three String methods.
RegExp Methods
Our tutorials will cover JavaScript's regular expressions methods. The following methods actually belong to the RegExp object.
- The exec() method returns an array of matched substrings.
- The test() method returns true if it matched a pattern in a string.
String Methods
The methods below belong to JavaScript's String object, but we chose to cover them along with RegExp methods since they rely on regular expressions.
- The String match() method returns an array of matched substrings.
- The String replace() method replaces a pattern matched by a substring argument.
- The String search() method returns the position index of a matched pattern.