CSS Unit 5
CSS Unit 5
Regular Expression,
Rollover and Frames
Contents
5.1. Regular Expression – Language of regular expression , finding
nonmatching characters, entering a range of characters, matching
digits and
nondigits, matching punctuation and symbols, matching words,
replace text using a regular expression, return the matched
characters, regular expression object properties
2) Using literal
Syntax - var pattern = /pattern/;
Example - var re2 = /xyz/;
5.1.1 – Language of Regular Expression
Property Description
g (global match) Find all matches rather than stopping after the
first match.
i (ignore case) Search is case-insensitive means no difference in
A and a.
m (multiline) Treat beginning and end characters (^ and $) as
working over multiple lines. In other words, match
the beginning or end of each line (delimited by \n
or \r)
s ("dotAll") Matches first letter in any newline.
2) Metacharacters
3) Quantifiers
5.1.1 – Language of Regular
Expression
1) Character Classes (Brackets)
– You can group characters by putting
them in Square brackets.
– hyphen(-) character inside bracket is
used to find a range of characters.e.g. [a-z]
– [...] matches any one of the characters
between the brackets e.g. [abc]
– [^...] matches any one character, but not
one of those inside the brackets e.g. [^abc]
– (x|y) matches any of the alternatives
specified.
5.1.1 – Language of Regular
Expression
Expression Description
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
Metacharact Description
er
. Find a single character, except newline or
line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
5.1.1 – Language of Regular
Expression
2) Metacharacters
– Metacharacters are characters with a special meaning within
the language of regular expression.
Metacharacter Description
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word,
beginning like this: \bHI, end like this: HI\b
\B Find a match, but not at the beginning/end of a
word
\0 Find a NULL character
Metacharac Description
ter
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal
number dd
\udddd Find the Unicode character specified by a
hexadecimal number
dddd
5.1.1 – Language of Regular
Expression
3) Quantifiers
– Quantifiers match a number of instances of a character, group,
or character
class in a string.
– The following table list the quantifiers:
Quantifier Description
* Match zero or more times.
+ Match one or more times.
? Match zero or one time.
{n} Match exactly n times.
{ n ,} Match at least n times.
{n,m} Match from n to m times.
5.1.1 – Language of Regular
Expression
3) Quantifiers
{n} - Exact count
– A number in curly braces {n} is the simplest quantifier. When you
append it to a
character or character class, it specifies how many characters or
character
classes you want to match.
<script>
var text = "hello JavaScripts123@";
var pattern = /[^a-z]/g;
var result = text.match(pattern);
document.write(result);
</script>
</body>
</html>
Matching Digits Example
<!DOCTYPE html>
<html>
<body>
<script>
var text = "hello JavaScripts123@";
var pattern =/\d/g;
var result = text.match(pattern);
document.write(result);
</script>
</body>
</html>
Matching Non-Digits Example
<!DOCTYPE html>
<html>
<body>
<script>
var text = "hello JavaScripts123@";
var pattern =/\D/g;
var result = text.match(pattern);
document.write(result);
</script>
</body>
</html>
Matching Punctions and symbols
<html>
<head>
<title> Regular Expression </title>
<body>
<script>
var myStr ="!";
var reg = /[*,#@+|!?.]/g;
var match = myStr.match(reg);
document.write(match);
</script>
</html>
Matching word example
<html>
<head>
<title> Regular Expression </title>
<body>
<script>
var myStr ="Welcome to Rcpp";
var reg = /\bRcpp\b/g;
var match = myStr.match(reg);
document.write(match);
</script>
</html>
Replace() method
The string.replace() is inbuilt method
in javascript,used to replace part of
string with new string.
By default, replace() method replaces
only first match.
Two types of replace
1. Using regular expression
2. Using normal string
5.2 – Frames
■ Frames are used for splitting up the browser
window into various panes
(sections), into which we can then load different
HTML documents.
<frameset rows="50%,50%">
<frame src="" frameBorder="1">
<frameset cols="50%,50%">
<frame src="" frameBorder="1">
</frameset>
</frameset>
</html>
Top and Parent property of
frame
Window or frame, that opens inside parent window is a
child window.
Child window/frame can be called or accessed from one
child window to another child window by using parent
property.
The parent property returns the parent window (of the
current window).
The parent property is read-only.
The parent property is not the same as the top property.
window.parent returns the immediate parent of a
window.
window.top returns the topmost window in the hierarchy
of windows.
Open, access new frame and its elements using
anchors
Example
Parentwin.html
<html>
<head>
<title>Parent Window</title>
</head>
<frameset cols="50%,50%">
<frame src="frame_1.html" name="f1">
<frame src="" name="f2">
</frameset>
</html>
Example (contd.)
Frame_1.html
<html>
<head>
<title> Child Window1 </title>
<body>
<h1> Frame 1 </h1>
<input type="text" name="text" value=""/><br><br>
<input type="button" name="button" value="click
me"/><br><br>
<a href="frame_2.html" target="f2">Call to frame 2</a>;
</body>
</html>
Example(contd)
Frame_2.html
<html>
<head>
<title> Child Window2 </title>
</head>
<body style="background-
color:powderblue;">
<h1> Frame 2 <br> It is child window</h1>
<frame framename="f2">
</body>
</html>
Rollover
Most web developers first use JavaScript for
rollovers.
■ A Rollover means a webpage changes when the
user moves mouse
cursor over and away from an object on the page.
■ It is often used in advertising.
■ It is often used in advertising.
■ A rollover may replace one image with another.
■ There are two ways to create rollover – using
plain HTML – using a mixture of JavaScript and
HTML
5.3.1 – Creating Rollover
onMouseOver
</body>
</html>
Text Rollover
Rollover can be created for text too.
Rolling over the specified text can be
used to have more information of text.
Such rollovers are used to make good
use of text and associated graphics, for
better understanding by users, making
webpage attractive as well as interactive.
Such rollovers can be created by simple
HTML as well as using javascript events
and functions.
Example rolling over text
<html>
<head>
<script>
MyFruits=new Array('apple.jpg','banana.jpg','orange.jpg')
fruit=0
function Show(fruit){
document.DisplayFruit.src=MyFruits[fruit] }
</script>
</head>
<body>
<img src="apple.jpg" name="DisplayFruit" "width=300, height=300"/></p>
<td><a onmouseover="Show(0)"
onmouseout="document.DisplayFruit.src='move.jpg'"><b>Show Apple</b></a><br><br>
<td><a onmouseover="Show(1)"
onmouseout="document.DisplayFruit.src='move.jpg'"><b>Show
Banana</b></a><br><br>
<td><a onmouseover="Show(2)"
onmouseout="document.DisplayFruit.src='move.jpg'"><b>Show
Orange</b></a><br><br>
</body>
</html>
5.3.3 – Multiple actions for
Rollover
It can be possible to perform multiple actions with
rollover using different mouse event handlers
- Onclickwhen mouse on an element
- OnmouseoverWhen cursor comes over element
- Onmouseout When cursor leaves an element
- Onmousedown When mouse button pressed
over element
- OnmouseupWhen mouse button released over
element
- OnmousemoveWhen mouse movement takes
place
5.3.4 – More efficient Rollover
When an image is accessed at first time,
second time or more using rollover, each time
it is loaded in browser.
This activity results in taking some time, and
the delay occurs for user in browsing.
To avoid this, using rollover once image is
loaded first time, it is stored in browser’s
cache in terms of variables.
When user accesses same image number of
times, it can be available without taking time
to load again as it is already loaded in
browser’s cache .
Example
<html> </head>
<head> <body>
<title> frame </title>
<img src="DSA.jpg"
height="200" width="200"
<script> name="cover">
dsa = new Image; <img src="" height="1"
width="1"><br>
wt = new Image;
<a
net = new Image; onmouseover="document.cover.
src=dsa.src">Elephant</a><br>
if(document.images)
<a
{ onmouseover="document.cover.
dsa.src ='elephant.jpg'; src=wt.src">Tiger</a><br>
wt.src ='tiger.jpg';
<a
onmouseover="document.cover.
net.src ='lion.jpg'; src=net.src">Lion</a><br>
} </body>
else </html>
{
dsa.src ='';
wt.src ='';
net.src ='';
}
</script>