CSS CH-5 Updated
CSS CH-5 Updated
Syntax
/pattern/modifiers;
Example
var patt = /spc/i;
Example explained:
/spc/i is a regular expression
spc is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier Description
g Perform a global match (find all matches rather than stopping after the first match)
test()
The test() method tests for a match in a string.
This method returns true if it finds a match, otherwise it returns false.
Syntax
RegExpObject.test(string)
Q] Write a program to check whether the string contains the given pattern or not.
<!DOCTYPE html>
<html>
</head>
<script type="text/javascript">
var p=/spc computer academy/i
function testMatch()
{
var str = T1.value;
if(p.test(str))
{
alert("String " + str + " contains the given pattern")
}
else
{
alert("String " + str + " does not contains the given pattern")
}
}
</script>
</head>
<body>
Brackets
Brackets ([]) have a special meaning when used in the context of regular expressions. They are
used to find a range of characters.
Sr.No. Expression & Description
1 [...]
Any one character between the brackets.
Ex: [abc]
2 [^...]
Any one character not between the brackets.
Ex: [^abc]
3 [0-9]
It matches any decimal digit from 0 through 9.
4 [^0-9]
Find any character NOT between the brackets (any non-
digit)
4 [a-z]
It matches any character from lowercase a through
lowercase z.
5 [A-Z]
It matches any character from uppercase A through
uppercase Z.
6 [a-Z]
It matches any character from lowercase a through
uppercase Z.
7 (x|y)
Find any of the alternatives specified
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.
Quantifiers
The frequency or position of bracketed character sequences and single characters can be denoted
by a special character. Each special character has a specific connotation. The +, *, ?, and $ flags all
follow a character sequence.
1 p+
It matches any string containing one or more p's.
2 p*
It matches any string containing zero or more p's.
3 p?
It matches any string containing at most one p.
4 p{N}
It matches any string containing a sequence of N p's
5 p{2,3}
It matches any string containing a sequence of two or three p's.
6 p{2, }
It matches any string containing a sequence of at least two p's.
7 p$
It matches any string with p at the end of it.
8 ^p
It matches any string with p at the beginning of it.
Metacharacter
A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the
combination a special meaning.
Metacharacter Description
\d Find a digit
\b Find a match at the beginning/end of a word, beginning like this: \bHI, end like
this: HI\b
RegExp Methods
Method Description
Returns an array containing all of the matches, including capturing groups, or null if
match()
no match is found.
Tests for a match in a string. It returns the index of the match, or -1 if the search
search()
fails.
Executes a search for a match in a string, and replaces the matched substring with a
replace()
replacement substring
Matching Words
<p>Enter some value in textbox and click on button to find characters NOT inside the
brackets.</p>
<script>
function myFunction() {
var patt1 = /student/gi;
var str = T1.value;
var result = str.match(patt1);
if(result!=null)
{
alert(" Entered string contains word student");
}
else
{
alert(" Entered string does not contain word student");
}
}
</script>
Enter string <input type="text" id="T1">
<button onclick="myFunction()">Try it</button>
</body>
</html>
The [^abc] expression is used to find any character NOT between the brackets.
^ symbol is placed at first position.
Ex: [^abc] It finds any non matching character i.e. any character except a,b,c.
<!DOCTYPE html>
<html>
<body>
<p>Enter some value in textbox and click on button to find characters NOT inside the
brackets.</p>
<script>
function myFunction() {
var patt1 = /[^ht]/gi;
var str = T1.value;
var result = str.match(patt1);
alert("Non Matching characters are "+ result);
}
</script>
Enter string <input type="text" id="T1">
<button onclick="myFunction()">Try it</button>
</body>
</html>
For example - to match a characters from ‘a’ to ‘d’ we must have a regular expression as [a-d]. Thus placing
the range within a square bracket helps us to evaluate a complete range of set of characters.
<!DOCTYPE html>
<html>
<body>
<p>Enter some value in textbox and click on button to find characters within the range.</p>
<script>
function myFunction() {
var patt1 = /[a-d]/gi;
var str = T1.value;
var result = str.match(patt1);
alert("characters within the range are "+ result);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
</head>
<script type="text/javascript">
var p=/\d/i
function testMatch()
{
var str = T1.value;
if(p.test(str))
{
alert("String " + str + " contains the digits")
}
else
{
alert("String " + str + " does not contains digits")
}
}
</script>
</head>
<body>
Enter string <input type="text" id="T1">
<button onclick="testMatch()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html>
</head>
<script type="text/javascript">
var p=/\D/g
function testMatch()
{
var str = T1.value;
if(p.test(str))
{
alert("Enter only Numbers")
}
else
{
alert("Correct Number")
}
}
</script>
</head>
<body>
Enter Phone number <input type="text" id="T1">
<button onclick="testMatch()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html>
</head>
<script type="text/javascript">
var p=/\W/i
function testMatch()
{
var str = T1.value;
if(p.test(str))
{
alert("String " + str + " contains the special character")
}
else
{
alert("String " + str + " does not contain special character")
}
}
</script>
</head>
<body>
Enter string <input type="text" id="T1">
<button onclick="testMatch()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
str="Welcome to Spc";
document.write(str);
function myfunction(str)
{
new_str=str.replace("Spc", "Spc Computer Academy");
document.write(new_str);
}
</script>
</head>
<body>
<br>
<input type="button" value="Replace" onclick="myfunction(str)">
</body>
</html>
Frames
HTML frames are used to divide your browser window into multiple sections where each section
can load a separate HTML document.
A collection of frames in the browser window is known as a frameset.
The window is divided into frames in a similar way the tables are organized: into rows and
columns.
Using multiple views we can keep certain information visible and at the same time other views
are scrolled or replaced.
Creating Frames
To use frames on a page we use <frameset> tag instead of <body> tag.
The <frameset> tag defines, how to divide the window into frames. The rows attribute of
<frameset> tag defines horizontal frames and cols attribute defines vertical frames.
Each frame is indicated by <frame> tag and it defines which HTML document shall open into the
frame.
Example1:
<frameset cols= “ 150, * ” >
will allow us to divide the window into two columns (i.e. in two vertical frames).
One frame occupying the size of 150 pixels and the other occupies the remaining portion of the
window.
Example 2:
<frameset rows= " *, 120 ”>
will divide the window into two rows ( i.e. in two horizontal frames).
The second part of horizontal Frame will be of 120 pixels and upper horizontal frame will occupy
remaining portion of the window.
cols
Specifies how many columns are contained in the frameset and the size of each column.
You can specify the width of each column in one of the four ways −
Absolute values in pixels. For example, to create three vertical frames, use cols = "100,
500, 100".
A percentage of the browser window. For example, to create three vertical frames,
use cols = "10%, 80%, 10%".
Using a wildcard symbol.
1
For example, to create three vertical frames, use cols = "10%, *, 10%". In this case
wildcard takes remainder of the window.
As relative widths of the browser window.
For example, to create three vertical frames, use cols = "3*, 2*, 1*". This is an alternative
to percentages.
You can use relative widths of the browser window. Here the window is divided into
sixths: the first column takes up half of the window, the second takes one third, and the
third takes one sixth.
rows
This attribute works just like the cols attribute and takes the same values, but it is used
2 to specify the rows in the frameset.
For example, to create two horizontal frames, use rows = "10%, 90%".
You can specify the height of each row in the same way as explained above for columns.
border
3 This attribute specifies the width of the border of each frame in pixels.
For example, border = "5". A value of zero means no border.
frameborder
4 This attribute specifies whether a three-dimensional border should be displayed
between frames. This attribute takes value either 1 (yes) or 0 (no). For example
frameborder = "0" specifies no border.
framespacing
This attribute specifies the amount of space between frames in a frameset. This can take
5 any integer value.
For example framespacing = "10" means there should be 10 pixels spacing between
each frames.
src
1 This attribute is used to give the file name that should be loaded in the frame. Its
value can be any URL. For example, src = "/html/top_frame.htm" will load an HTML
file available in html directory.
name
This attribute allows you to give a name to a frame. It is used to indicate which frame
2 a document should be loaded into. This is especially important when you want to
create links in one frame that load pages into an another frame, in which case the
second frame needs a name to identify itself as the target of the link.
frameborder
3 This attribute specifies whether or not the borders of that frame are shown; it
overrides the value given in the frameborder attribute on the <frameset> tag if one
is given, and this can take values either 1 (yes) or 0 (no).
marginwidth
This attribute allows you to specify the width of the space between the left and
4
right of the frame's borders and the frame's content. The value is given in pixels.
For example marginwidth = "10".
marginheight
5 This attribute allows you to specify the height of the space between the top and
bottom of the frame's borders and its contents. The value is given in pixels. For
example marginheight = "10".
noresize
6 By default, you can resize any frame by clicking and dragging on the borders of a
frame. The noresize attribute prevents a user from being able to resize the frame. For
example noresize = "noresize".
scrolling
7 This attribute controls the appearance of the scrollbars that appear on the frame.
This takes values either "yes", "no" or "auto". For example scrolling = "no" means it
should not have scroll bars.
Example 1:
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<noframes>
<body>Your browser does not support frames.</body>
</noframes>
</frameset>
</html>
Example 2:
</html>
<!DOCTYPE html>
<html>
<head>
<title>HTML Target Frames</title>
</head>
<noframes>
<body>Your browser does not support frames.</body>
</noframes>
</frameset>
</html>
Rollover
Rollover means change in the appearance of the object, when user moves his or her mouse over an
object on the page
The rollover effect is mainly used in web page designing for advertising purpose.
Creating Rollover
On many web pages, JavaScript rollovers are handled by adding an onmouseover and onmouseout
event on images.
Image Rollover
<html>
<head>
<title>Creating Rollover</title>
</head>
<body>
<img src="mickey1.jpeg" boarder="0px" width="650px" height="550px" onmouseover="src=
'mickey.jpeg'" onmouseout="src= 'mickey1.jpeg'">
</body>
</html>
Text Rollover
Text rollover is a technique in which whenever user rollover the text, JavaScript allows to change the page
element usually some graphics image.
Carefully placed rollovers can enhance a visitors experience when browsing the web page.
<html>
<body>
<a>
<img src ="mango.jpeg" name="fruit" width="650px" height="550px">
</a>
<a onmouseover="document.fruit.src='mango.jpeg'">
<br>
<b><u>Mango</u></b>
</a>
<br><br><br>
<a onmouseover="document.fruit.src='banana.jpeg'">
<b><u>banana</u></b>
</a>
<br><br><br>
<a onmouseover="document.fruit.src='pineapple.jpeg'">
<b><u>pineapple</u></b>
</a>
<br><br/><br/>
</body>
</html>
<script type="text/javascript">
function display1()
{
document.fruit.src='mango.jpeg'
document.getElementById('para').innerHTML="Trees of mango are larger than
Pineapple"
}
function display2()
{
document.fruit.src='pineapple.jpeg'
document.getElementById('para').innerHTML="Trees of pineapple are smaller than
mango"
}
</script>
</head>
<body>
<a onmouseover="display1()">
<br>
<b><u>mango</u></b>
</a>
<br>
<a onmouseover="display2()">
<br>
<b><u>Pineapple</u></b>
</a>
<br><br><br>
<a>
<img src ="mango.jpeg" name="fruit" width="650px" height="550px">
</a>
<p id="para">Trees of mango are larger than Pineapple </p>
</body>
</html>