Understanding Regular Expressions Regular expressions are, to put it simply, patterns that can be matched with strings. A pattern in a regular expression contains a string that can be searched for in a larger string. However, this can also be done (faster) using indexOf(). The advantage of regular expressions is that some special features such as wildcards are available. Table 2.3 shows some special characters and their meanings. Table 2.3. Special Characters in Regular ExpressionsSpecial Character | Description | Example |
---|
^ | Beginning of the string | ^a means a string that starts with a | $ | End of the string | a$ means a string that ends with a | ? | 0 or 1 times (refers to the previous character or expression) | ab? means a or ab | * | 0 or more times (refers to the previous character or expression) | ab* means a or ab or abb or… | + | 1 or more times (refers to the previous character or expression) | ab+ means ab or abb or abbb or… | [...] | Alternative characters | PHP[45] means PHP4 or PHP5 | - (used within square brackets) | A sequence of values | ECMAScript [3-5] means ECMAScript 3 or ECMAScript 4 or ECMAScript 5 | ^ (used within square brackets) | Does match anything but the following characters | [^A-C] means D or E or F or… | | | Alternative patterns | ECMAScript 3|ECMAScript 4 means ECMAScript 3 or ECMAScript 4, as does ECMAScript (3|4) | (...) | Defines a subpattern | (a)(b) means ab, but with two subpatterns (a and b) | . | Any character | . means a, b, c, 0, 1, 2, $, ^,… | {min, max} | Minimum and maximum number of occurrences; if either min or max is omitted, it means 0 or infinite | a{1,3} means a, aa, or aaa. a{,3} means empty string, a, aa, or aaa. a{1,} means a, aa, aaa,… | \ | escapes the following character | \. stands for . |
Other special characters and expressions are available, for instance, a character that refers to a digit (\d). |