UNIT V
UNIT V
A regular expression is a sequence of characters that forms a search pattern.When you search
for data in a text, you can use this search pattern to describe what you are searching for.A regular
expression can be a single character, or a more complicated pattern.Regular expressions can be
used to perform all types of text search and text replace operations.
Note: If you are already familiar with the forms of a regular expression, you may also read the
cheat sheet for a quick lookup for a specific pattern/construct.
Simple patterns are constructed of characters for which you want to find a direct match. For
example, the pattern /abc/ matches character combinations in strings only when the exact
sequence "abc" occurs (all characters together and in that order). Such a match would succeed in
the strings "Hi, do you know your abc's?" and "The latest airplane designs evolved from
slabcraft." In both cases the match is with the substring "abc". There is no match in the string
"Grab crab" because while it contains the substring "ab c", it does not contain the exact substring
"abc".
When the search for a match requires something more than a direct match, such as finding one or
more b's, or finding white space, you can include special characters in the pattern. For example,
to match a single "a" followed by zero or more "b"s followed by "c", you'd use the pattern /ab*c/:
the * after "b" means "0 or more occurrences of the preceding item." In the string
"cbbabbbbcdebc", this pattern will match the substring "abbbbc".
The following pages provide lists of the different special characters that fit into each category,
along with descriptions and examples.
Assertions
Assertions include boundaries, which indicate the beginnings and endings of lines and words,
and other patterns indicating in some way that a match is possible (including look-ahead, look-
behind, and conditional expressions).
Character classes
Distinguish different types of characters. For example, distinguishing between letters and digits.
Groups and ranges
Indicate groups and ranges of expression characters.
Quantifiers
Indicate numbers of characters or expressions to match.
Unicode property escapes
Distinguish based on unicode character properties, for example, upper- and lower-case letters,
math symbols, and punctuation.
Regular expressions are used with the RegExp methods test() and exec() and with the String
methods match(), replace(), search(), and split(). These methods are explained in detail in the
JavaScript reference.
When you want to know whether a pattern is found in a string, use the test() or search() methods;
for more information (but slower execution) use the exec() or match() methods. If you use exec()
or match() and if the match succeeds, these methods return an array and update properties of the
associated regular expression object and also of the predefined regular expression object,
RegExp. If the match fails, the exec() method returns null (which coerces to false).
Syntax
or
Pattern: A string that specifies the pattern of the regular expression or another regular
expression.
Attributes: An optional string containing any of the "g", "i", and "m" attributes that
specify global, case-insensitive, and multiline matches, respectively.
Modifiers
Modifier Description
Brackets
[^abc]
[0-9]
[^0-9]
Metacharacters
Metacharacter Description
\d Find a digit
Quantifiers
Property Description
constructor Returns the function that created the RegExp object's prototype
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
Method Description
compile() Compiles a regular expression
exec() Tests for a match in a string. Returns the first match
test() Tests for a match in a string. Returns true or false
toString() Returns the string value of the regular expression
In JavaScript, regular expressions are often used with the two string methods: search() and
replace().
The search() method uses an expression to search for a match, and returns the position of
the match.
The replace() method returns a modified string where the pattern is replaced.
The search() method searches a string for a specified value and returns the position of the match:
Example
Use a string to do a search for "W3schools" in a string:
var str = "Visit W3Schools!";
var n = str.search("W3Schools");
The replace() method replaces a specified value with another value in a string:
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
Example
Use a case insensitive regular expression to replace Microsoft with W3Schools in a string:
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");
The result in res will be:
Visit W3Schools!
In JavaScript, the RegExp object is a regular expression object with predefined properties and
methods.
Using test()
You don't have to put the regular expression in a variable first. The two lines above can be
shortened to one:
/e/.test("The best things in life are free!");
Using exec()
Rollover:
The keyword that is used to create rollover is the <onmousover> event. For example, we want to
create a rollover text that appears in a text area. The text “What is rollover?” appears when the
user place his or her mouse over the text area and the rollover text changes to “Rollover means
a webpage changes when the user moves his or her mouse over an object on the page” when the
user moves his or her mouse away from the text area. The HTML script is shown in example 1
Example 1
<HTML>
<head></head>
<Body>
<textarea rows=”2″ cols=”50″ name=”rollovertext” onmouseover=”this.value=’What is
rollover?'”
onmouseout=”this.value=’Rollover means a webpage changes when the user moves his or
her mouse over an object on the page'”>
</textarea>
</body>
</html>
Example 2
<p
onmouseover=”this.style.color=’red'”
onmouseout=”this.style.color=’blue'”>
Move the mouse over this text to change its color to red. Move the mouse away to
change the text color to blue.
</p>
Example 3
<html>
<head>
<title>Rollover Effect</title>
</head>
<body>
<table>
<tbody>
<tr valign=”top”>
<td width=”50″>
<a><img src=”vb2010book.jpg” name=”book”></a>
</td>
<td><img height=”1″ width=”10″></td>
<td><a onmouseover=”document.book.src=’vb2010book.jpg'”><b>Visual Basic 2010 Made
Easy</b></a>
<br>
<a onmouseover=”document.book.src=’vb2008book.jpg'”><b>Visual Basic 2008 Made
Easy</b></a>
<br>
<a onmouseover=”document.book.src=’vb6book.jpg'”><b>Visual Basic 6 Made Easy</b></a>
<br>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Example 4
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<script language=”Javascript”>
MyBooks=new Array(‘vb2010book.jpg’,’vb2008book.jpg’,’vb6book.jpg’)
book=0
function ShowCover(book){document.DisplayBook.src=MyBooks[book]
}</script></head>
<body>
<body>
<P align=”center”><img src=”vb2010book.jpg” name=”DisplayBook”/><p>
<center>
<table border=0>
<tr>
<td align=center><a onmouseover=”ShowCover(0)”><b>Visual Basic 2010 Made
Easy</b></a><br>
<a onmouseover=”ShowCover(1)”><b>Visual Basic 2008 Made Easy</b></a><br>
<a onmouseover=”ShowCover(2)”><b>Visual Basic 6 Made Easy</b></a><br>
</td>
</tr>
</table>
</body>
</html>
Frames:
There are no official standards for the Browser Object Model (BOM).Since modern browsers
have implemented (almost) the same methods and properties for JavaScript interactivity, it is
often referred to, as methods and properties of the BOM.
The window object is supported by all browsers. It represents the browser's window. All global
JavaScript objects, functions, and variables automatically become members of the window
object.
Even the document object (of the HTML DOM) is a property of the window object:
window.document.getElementById("header");
document.getElementById("header");
Window Size
Two properties can be used to determine the size of the browser window.Both properties return
the sizes in pixels:
The browser window (the browser viewport) is NOT including toolbars and scrollbars.
or
document.body.clientHeight
document.body.clientWidth
Example
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;