|
Partners
|
Programmer's Guide to Regular ExpressionsCredits: This tutorial is written by David Andersson (Liorean). Please see footnote for more information on author. What is a regular expression?Regular expressions is a form of pattern matching that you can apply on textual content. Take for example the DOS wildcards ? and * which you can use when you're searching for a file. That is a kind of very limited subset of RegExp. For instance, if you want to find all files beginning with "fn", followed by 1 to 4 random characters, and ending with "ht.txt", you can't do that with the usual DOS wildcards. RegExp, on the other hand, could handle that and much more complicated patterns. Regular expressions are, in short, a way to effectively handle data, search and replace strings, and provide extended string handling. Often a regular expression can in itself provide string handling that other functionalities such as the built-in string methods and properties can only do if you use them in a complicated function or loop.
|
|||||||||||||||||||||||||
| Syntax | Example |
|---|---|
| RegExp Literal | |
/pattern/flags; |
var re = /mac/i; |
| RegExp Object Constructor | |
new RegExp("pattern","flags"); |
var re = new RegExp(window.prompt("Please
input a regex.","yes|yeah"),"g"); |
There are three flags that you may use on a RegExp. The multiline flag is supported only in JavaScript1.5+, but the other two are supported in pretty much every browser that can handle RegExp (JavaScript.1.2+). These flags can be used in any order or combination, and are an integral part of the RegExp.
| Flag | Description |
|---|---|
| Global Search | |
g |
The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern. |
| Ignore Case | |
i |
The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as å, ü, ñ, æ. |
| Multiline Input | |
m |
This flag makes the beginning of input (^) and end
of input ($) codes also catch beginning and end of line
respectively. JavaScript1.5+ only. |