Regular Expressions

A regular expression object describes a pattern of characters, which can be matched agaist strings.


Syntax

var name= /pattern of characters/


In the following text we are going to use a regular expression with the method
match() to find the word apple.

<script>
var fruit= "apple,banana,orange,pineapple";
var find=/apple/;
document.write(fruit.match(find));
</script>

Displays



You can use the regular expressions without declaring a variable - let us find the word orange.

<script>
var fruit= "apple,banana,orange,pineapple";
document.write(fruit.match(/orange/));
</script>

Displays




Modifiers

Modifiers Explanation
gused for a global match - finds all the matches within the string
i used for case-insensitive matching
m used for searching across multiple lines of text



<script>
var fruit= "apple,banana,orange,pineapple";

//g performs a global match
document.write(fruit.match(/apple/g));
</script>

Displays



<script>
var fruit= "apple,banana,orange,pineapple";

//i performs case-insensitive match
document.write(fruit.match(/BANANA/i));
</script>

Displays



The tables below show some of the common regular expressions that are used.



Expression

Expression Explanation
\d matches a single numberic character
\D matches a single character that is not a numeric chracter
\n matches a new line
\t matches a tab
\s matches a white space character
\d matches a digit
\w matches any single character that is a letter, number, or underscore
\f matches a form feed
\t matches a tab
\r matches a carriage return
\b matches a word boundary - the start or end of a word
\B matches a position that is not located at a word boundary



<script>
var time= "one day I went to the museum";

document.write(time.match(/\bmuse/g) +"<br>")

document.write(time.match(/\bda/g) +"<br>")

document.write(time.match(/\Buse/g))
</script>

Displays






Metacharacter

Metacharacter Explanation
[0-9] matches any digits from 0 through to 9
[a-z] matches any lowercase characters from a through to z
[A-Z] matches any uppercase characters from A through to Z
^ matches character from the begining of a line
$ matches characters from the end of a line
. matches any single character except line break characters



<script>
var mix= "mkabgloMTU234562";
document.write(mix.match(/[M]/g) +"<br>");
document.write(mix.match(/[a-l]/g)+"<br>");
document.write(mix.match(/[2]/g)+"<br>");
</script>

Displays



Quantifiers

Methods Explanation
* x* matches any string containing zero or more x's - it can be any characters
+ x+ must match any string containing at least one x - it can be any characters
? x? matches any string containing zero or one x - it can be any characters
{n} x{2} matches any string containing a sequence of 2 x's
{n,n1} x{5, 6} matches any string containing a sequence of five or six x's



<script>
var alp= "mitekittenbitten";
document.write(alp.match(/t{1}/g) +"<br>:")
document.write(alp.match(/t{2}/g)+"<br>:")
document.write(alp.match(/t{1,2}/g));
</script>

Displays




Now do the Exercise!