Regular Expression

A regular expression is the set of rules that define a pattern. A pattern is expressed between the delimiters / ... /

The String method search() returns the position of the first occurence of the pattern if it exists or -1 if it does not.

In its simplest form a pattern could be a string of characters: /abc/. str.search(/abc/) will return the position of the first occurrence of the string "abc" in str or -1 if the pattern does not exist. If you want the pattern to be case insensitive the add the character i at the end, like so /abc/i

The following are meta characters in a regular expression.

. * + ? ^ $ [ ] ( ) { } | \
To escape the special significance of these characters in a regular expression the back slash (\) is used.

The period . is a placeholder for a single character other than the new line character.

To denote a range the square brackets are used. For example [aeiou] will match any vowel character. The hyphen is used as a shorthand like [a-z] which represents any lower case letter. The caret (^) indicates not in the range. [^0-9] will match any character that is not a digit.

The following escape sequences denote pre-defined ranges:

\d Any digit [0 - 9]
\D Not a digit [^0 - 9]
\w Any alphanumeric character and the underscore character
\W Not an alphanumeric character
\s White space, tab, or newline
\S Not a white space, tab, or newline character

One can denote the position of a pattern within a string. The caret (^) denotes the beginning of the string. The pattern /^abc/ will match a string starting with abc. The dollar sign ($) denotes the end of the string. The pattern /xyz$/ will match a string that ends with xyz. \b denotes word boundary and \B denotes not a word boundary.

There are several ways to denote a recurring pattern:

The parenthesis () is used to group patterns. To indicate a choice of patterns we can write something like - /(abc)|(def)|(ghi)/. This will match a pattern with abc or def or ghi.

There are several functions in the String class that use regular expressions as input parameter. For a given String s here is how you would use a regular expression regex in these methods:

s.match (regex) Returns the part of the string that matches the regular expression
s.split (regex) Returns an array of substrings of s divided at every occurence of the regular expression. The regular expression is not included as a part of the substrings
s.search (regex) Returns the position of the first match the regular expression and -1 if there is no match.

Here is an online Regular Expression tester that you can use to try out your regular expression skills: