Chapter 5 Regular Expressions, Rollover and Frames Regular Expression
Chapter 5 Regular Expressions, Rollover and Frames Regular Expression
Regular Expression
• When you search for data in a text, you can use this search pattern to describe what
you are searching for.
• Regular expressions can be used to perform all types of text search and text replace
operation
Syntax:
or simply
Where
• 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 multi-line matches, respectively.
Ex:
• The words of the regular expression language are called special characters and act
similarly to an operator in a mathematical expression.
• If you want to search for a symbol that is also a special character, you must precede
the symbol with a backslash (\), which is known as an escape character.
• The backslash tells the browser to ignore the special meaning of the symbol.
Here's what you'd need to write to search for the ‘[‘ symbol in text.
• Ex: / [ \ [ ] /
1. The ‘/’ character tells the browser that this is the beginning of a regular expression.
2. The ‘[‘ character tells the browser to search the text for the following character(s).
3. The ‘\’ tells the browser to ignore the special meaning of the next character.
4. The ‘[‘ character is the character that the browser will search for in the text.
5. The ‘]’ character tells the browser that there are no more characters to search for.
6. The ‘/’ character tells the browser that this is the end of the regular expression.
Quantifiers
• The frequency or position of bracketed character sequences and single characters can
be denoted by a special character.
Literal Characters:
3. \t Tab (\u0009)
4. \n Newline (\u000A)
8. \xnn The Latin character specified by the hexadecimal number nn; for
example, \x0A is the same as \n
9. \uxxxx The Unicode character specified by the hexadecimal number xxxx;
for example, \u0009 is the same as \t
10. \cX The control character ^X; for example, \cJ is equivalent to the
newline character \n
Meta characters:
• For instance, you can search for a large sum of money using the '\d' metacharacter:
/([\d]+)000/, Here \d will search for any string of numerical character.
The following table lists a set of metacharacters which can be used in PERL Style
Regular Expressions
Explanation:
3. \S non-whitespace character
4. \d a digit (0-9)
5. \D a non-digit
7. \W a non-word character
• You can direct the browser to search for illegal character(s) by specifying the illegal
character(s) within brackets and by placing the caret (^) as the first character in the
bracket.
/[ ^ \- ]/
• In this case, the browser is asked to determine whether the text does not contain the
hyphen.
• The caret asks the browser to determine whether the following character(s) do not
appear in the text.
• Brackets ([]) have a special meaning when used in the context of regular expressions.
They are used to find a range of characters.
The ranges shown above are general; you could also use the range [0-3] to match any
decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character
ranging from b through v
• Limiting an entry either to digits or nondigits is a common task for many JavaScript
applications.
• For example, a telephone number entered by a user should be a series of digits, and a
first name should be nondigits.
• You can have the browser check to see whether the text has digits or nondigits by
writing a regular expression.
• The regular expression must contain either \d or \D, depending on whether you want
the browser to search the text for digits (\d) or nondigits (\D).
• / \d / :This symbol, tells the browser to determine whether the text contains digits.
The browser returns a true if at least one digit appears in the text.
• / \D /: This symbol is used to tell the browser to search for any nondigit in the text.
This is illustrated next. The browser returns a true if a nondigit is found.
Matching Punctuation and Symbols:
• The browser determines whether text contains or doesn't contain letters, punctuation,
or symbols, such as the @ sign in an e-mail address
• The \w special symbol tells the browser to determine whether the text contains a
letter, number, or an underscore.
• The \W special symbol reverses this request by telling the browser to determine
whether the text contains a character other than a letter, number, or underscore.
• Ex: You can use the following regular expression to determine whether the product
name that was entered into the form on your web page contains a symbol:
Matching Words:
• You might want the browser to search for a particular word within the text.
• A word is defined by a word boundary that is, the space between two words.
• You define a word boundary within a regular expression by using the \b special
symbol.
• The \b special symbol as a space between two words. You need to use two \b special
symbols in a regular expression.
• If you want the browser to search for a word: the first \b represents the space at the
beginning of the word and the second represents the space at the end of the word.
• You want to determine whether the name Bob appears in the text.
• Since you don't want the browser to match just text that contains the series of letters
B-o-b, such as Bobby, you'll need to use the word boundary to define Bob as a word
and not simply a series of letters.
• Ex:
<script>
function RegExpression() {
var name='Bob'
re = /[bt]/
if (re.test(name))
{ alert('Found') }
else
{ alert('Not Found') }
</script>
Output:
• A true is returned if either b or t or both are found in the name Bob; otherwise a false
is returned.
• Depending on this result, the appropriate alert dialog box is displayed on the screen.
re = / \b Bob \b /
Placing ‘g’ at the end of the regular expression, which tells the browser to replace all
occurrences of the regular expression in the string.
Steps:
1. Create a regular expression that identifies portion of text that you want replaced.
2. Determine the replacement text. Pass both of these to the replace() method, and
browser follows the direction given in the regular expression to locate the text.
3. If the text is found, the browser replaces it with the new text that you provided.
4. The next example tells the browser to replace Bob with Mary in the text.
6. The replace() method of the string object is then called to use the regular expression
to search for Bob within the text and then replace Bob with Mary.
7. However, the original string isn't modified. The modified string is returned by the
replace() method.
Replacing Like Values:
• A regular expression can be written to search for variations of a name and replace it
with a standardized name.
• To do this, the regular expression must contain literal characters and wildcard
characters.
• A literal character is a letter, number, or symbol that must match exactly within the
text.
• A wildcard is a special symbol that tells the browser to accept one or multiple
unspecified characters as a match.
• Two types of wildcards can be used: a period (.) and an asterisk (*).
• The period tells the browser to match any single character, while the asterisk indicates
zero or more occurrences of whatever precedes it.
• Ex: The following matches Bob but not Bobby, because a single wildcard character is
used:
• / Bob. /
• However, this regular expression matches both Bob and Bobby because the multiple
character wildcard is used:
• / Bob.* /
• Sometimes you need to retrieve characters that match a regular expression rather than
simply testing whether those characters exist in the text or not.
• You can have the browser return characters that match the pattern in your regular
expression by calling the exec() method of the regular expression object.
The exec() method returns an array. The first array element contains the text that matches
your regular expression
• Ex: The following regular expression matches Bob and any word that begins with B-
o-b.
/ \b Bob.* \b /
1. Create the regular expression object and assign it the regular expression: re = / \b
Bob.* \b /
2. Call the exec() method, passing it the text and assigning the return value to an array
variable. re = / \b Bob.* \b /
re = / \b Bob.* \b /
2. $_ Same as input
3. $* Same as multiline
15. leftContext The substring to the left of the most recent match
18. rightContext The substring to the right of the most recent match
1. constructor: It returns a reference to the array function that created the instance's
prototype.
Syntax
RegExp.constructor
Return Value
Example:
<html>
<head>
<title>JavaScript RegExp constructor Property</title>
</head>
<body>
</script>
</body>
</html>
Syntax
RegExpObject.global
Return Value
Example:
<html>
<head></head>
<body>
</script>
</body>
</html>
Syntax
RegExpObject.ignoreCase
Return Value
Example:
<html>
<head></head>
<body>
</script>
</body>
</html>
re.test(str);
document.write("Test 1 - Current Index: " + re.lastIndex);
re.test(str);
document.write("<br />Test 2 - Current Index: " + re.lastIndex);
</script>
</body>
</html>
Frames:
• Many times a web page is distributed in number of sections.
• Although this looked as though it were all contained on a single web page, actually
multiple web pages appeared on the screen at the same time, and each was displayed
in a frame.
• Frames are created using HTML, but you can interact and manipulate frames using a
JavaScript.
• All frames contain at least three web pages.
• The first frame surrounds the other frames, and this entire collection is called the
frameset.
• The other frames are within the frameset, and each is referred to as a child.
<frameset>:
• The number of rows or columns that appear in a frameset is determined by the value
assigned to these attributes.
• Each column or row is represented by a percentage that indicates the percent of the
frameset that is taken up by the column or row.
• You can also specify a width and height—it doesn't have to be a percentage of the
available window.
• Ex: <frameset rows="50%,50%">
• It divides the frame horizontally into two child windows evenly.
<frame> tag:
• After you define the frameset, you can insert a web page into each child window.
• This can be done by using the <frame> HTML tag.
• Each child window has its own <frame> tag.
• You specify the web page that will be displayed in the child window by defining a
value for the src attribute of the <frame> tag.
• A unique name can also be specified for the child window by assigning the name to
the name attribute of the <frame> tag
• Ex: <frame src="WebPage1.html" name="topPage" />
• You'll need to define a <frame> tag within the <frameset> tag for each child window
contained in the <frameset> tag.
• The first <frame> tag within the <frameset> tag refers to the upper left–most child
window.
• Subsequent <frame> tags refers to child windows that appear left to right, top to
bottom within the <frameset> tag.
• Ex:
• <!—Dividing a Screen using <franeset>
• <html>
• <head> </head>
• <frameset rows="50% , 50%">
• <frame src="WebPage1.html" name="topPage" />
• <frame src="WebPage2.html" name="bottomPage" />
• </frameset>
• </html>
Invisible Borders:
• It is possible to use frames by hiding the borders around the child windows within
your frameset.
• The border can be hidden by setting the frameborder and border attributes of the
<frame> tag to zero (0).
Ex:
<html>
<head></head>
<frameset rows="50%,50%">
<frame src="WebPage1.html" name="topPage"
frameborder="0" border="0" />
<frame src="WebPage2.html" name="bottomPage"
frameborder="0" border="0" />
</frameset> </html>
Rollover:
• Rollovers are used to make a web page alive, by altering its appearance as the visitor
scans the contents of the web page with the mouse.
• Any object on a web page can be changed with a rollover.
• Rollovers are commonly used on product pages to display details about merchandise
for sale online.
Creating Rollover:
• A rollover is caused by an event called onmouseover and occurs when a visitor to
your web site moves the mouse over an object that appears on the page.
• An object can be an image, text, or any element of a form.
Ex: <IMG height="92" src="7441805.gif" width="70"
border="0" onmouseover="src='0072253630.jpeg'">
• Here, the original image is the 7441805.gif file. The new image is the
0072253630.jpeg file.
• The onmouseover attribute is assigned the complete assignment statement
(src='0072253630.jpeg'), which tells the browser to replace the 7441805.gif image
with 0072253630.jpeg.
Creating Rollback:
• you'll want to roll back, or reverse changes, of the onmouseover event when the
visitor moves the cursor away from the object.
• For example, you may want the original image to return to the screen after the mouse
is moved away, replacing the image that was displayed when the onmouseover event
occurred.
• You can do this by reacting to the onmouseout event, which occurs whenever the
mouse cursor is moved off an object.