CSS Unit 5
CSS Unit 5
Regular Expression
3. Modifiers
Modifiers are special flags that you can add to a regular expression to change how it works.
They help you control how regex searches through text.
Common modifiers:
i (Case insensitive): Makes the search ignore letter case. For example, /cat/i matches
with cat, CAT, cAt, Cat, Cat, etc.
m (multi – line): Changes how the ^ and $ characters work. With this modifier, ^
matches with the start of each line and $ matches with the end of each line. For example:
/^Hello/m;
g (global): It tells the regex to find all the matches in the text, not just the first one.
Without this modifier, regex stops after finding the first match. For example: /cat/g;
4. Literal Characters
They are the actual characters to be matched exactly as they appear.
Single letters (Example: “a”)
Whole words (Example: “cat”)
Special characters: Some characters are special in regex, like ‘.’, ‘,’, ‘*’, ‘+’ etc. If you
want to match them literally, you need to escape them with a backslash.
Example:
var pattern = /\w + @\w +\.\w +/g;
var text = “Contact us at [email protected] or [email protected].”;
var matches = text.match(pattern);
console.log(matches); //[[email protected], [email protected]]
5. Metacharacters
A metacharacter is simply an alphabetical character preceded by a backslash that acts
to give the combination a special meaning.
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.
Character Description
. A single character
\s A whitespace character (space, tab, newline)
\S A non-whitespace character
\d A digit (0-9)
\D A non-digit
\w A word character (a-z, A-Z, 0-9,..)
\W A non-word character
[\b] A literal backspace character special case
[aeiou] Matches a single character in the given set
[^aeiou] Matches a single character outside the given set
foo | bar | baz Matches any one of the alternatives specified
6. RegExp Methods
Here is a list of the methods associated with RegExp along with their description.
Method Description
compile() Compiles a regular expression. Deprecated in version 1.5
exec() Tests for a match in a string. Returns the first match.
test() Tests for a match in a string. Return s true or false.
toString() Returns the string value of the regular expression.
<html>
<body>
<form name="form1">
<input placeholder="Enter here" id="name1">
<input type="button" value="Click here to Validate" onclick="validate()">
<p id="demo"></p>
</form>
<script>
function validate()
{
var str = document.getElementById("name1").value;
var pattern = /^[^aeiou]/i;
if (pattern.test(str))
document.getElementById("demo").innerHTML = "You Entered Consonant";
else
document.getElementById("demo").innerHTML = "You Entered Vowel";
}
</script>
</body>
</html>
To show a range of characters, use square brackets and separate the starting character from the
ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be
put inside square brackets. For example, [A-CX-Z] matches 'A' or 'B' or 'C' or 'X' or 'Y' or 'Z'.
[a-z] includes all lower-case alphabetic characters because those characters appear in sequence
in ASCII. Similarly, [A-Z] includes all upper-case alphabetic characters because those
characters appear in sequence in ASCII.
<html>
<body>
<form name="form1">
Chapter Name: <input id="Cname">
<p id="p1"></p>
Chapter Number: <input id="Cnumber">
<p id="p2"></p><br>
<input type="button" value="Click here to Validate" onclick="validate()">
</form>
<script>
function validate()
{
var name=document.getElementById("Cname").value;
var number=document.getElementById("Cnumber").value;
var pattern_name = /^[a-zA-Z]+/;
var pattern_number=/^[1-6]/;
if(pattern_name.test(name)&&pattern_number.test(number))
{
document.getElementById("p1").innerHTML ="Valid";
document.getElementById("p2").innerHTML ="Valid";
}
else
{
document.getElementById("p1").innerHTML ="Invalid";
document.getElementById("p2").innerHTML ="Invalid";
}
}
</script>
</body>
</html>
Matching digits and non – digits
<html>
<body>
<form name="form1">
Enter Digits: <input id="digit"> <p id="p1"></p>
Enter Non-digits: <input id="non-digit"> <p id="p2"></p><br>
<input type="button" value="Click here to Validate" onclick="validate()">
</form>
<script>
function validate()
{
var d=document.getElementById("digit").value;
var D=document.getElementById("non-digit").value;
var pattern1_digit = /[0-9]+/;
var pattern2_digit=/\d/;
var pattern1_not_digit = /[^0-9]+/;
var pattern2_not_digit=/\D/;
if(pattern1_digit.test(d)||pattern2_digit.test(d))
document.getElementById("p1").innerHTML ="Valid";
else
document.getElementById("p1").innerHTML ="Invalid";
if(pattern1_not_digit.test(D)||pattern2_not_digit.test(D))
document.getElementById("p2").innerHTML ="Valid";
else
document.getElementById("p2").innerHTML ="Invalid";
}
</script>
</body>
</html>
Matching punctuations and symbols
It is all non-word and non-space characters. This includes characters such as periods, commas,
exclamation marks, and quotation marks. For example, !, @, #, $, %, ^, &, *, (, ), _, +, |, }, {,
:, ”, ?, >, <, \, ], [, ‘, ;, ., /, ,.
<html>
<body>
<form name="form1">
Enter String: <input id="p">
<p id="p1"></p>
<input type="button" value="Click here to Validate" onclick="validate()">
</form>
<script>
function validate()
{
var str=document.getElementById("p").value;
var pattern_punc = /[!@#$%^&*\(\)_\+{}|":<>\?\[\]\;'/\.,"]+/g;
if(pattern_punc.test(str))
document.getElementById("p1").innerHTML ="String Contains punctuations
and symbol";
else
document.getElementById("p1").innerHTML ="String not Contains
punctuations and symbol";
}
</script>
</body>
</html>
<html>
<body>
<form name="form1">
<input type="button" value="constructor" onclick="constructor()">
<input type="button" value="global" onclick="global()">
<input type="button" value="ignoreCase" onclick="ignoreCase()">
<input type="button" value="lastIndex" onclick="lastIndex()">
<input type="button" value="Multiline" onclick="multiline()">
<input type="button" value="source" onclick="source()"><br>
<p id="p2"></p>
<p id="p1"></p>
</form>
<script>
function constructor()
{
var pattern = new RegExp("JavaScript","ig");
var r=pattern.constructor;
document.getElementById("p1").innerHTML =r;
document.getElementById("p2").innerHTML ="Returns the function that created the
RegExp object's prototype";
}
function global()
{
var pattern = new RegExp("JavaScript","ig");
var r1=pattern.global;
document.getElementById("p1").innerHTML =r1;
document.getElementById("p2").innerHTML ="Checks whether the 'g' modifier is
set";
}
function ignoreCase()
{
var pattern = new RegExp("JavaScript","ig");
var r2=pattern.ignoreCase;
document.getElementById("p1").innerHTML =r2;
document.getElementById("p2").innerHTML ="Checks whether the 'i' modifier is
set";
}
function lastIndex()
{
var pattern = new RegExp("JavaScript","ig");
var r3=pattern.lastIndex;
document.getElementById("p1").innerHTML =r3;
document.getElementById("p2").innerHTML ="Specifies the index at which to start
the next match";
}
function Multiline()
{
var pattern = new RegExp("JavaScript","ig");
var r4=pattern.multiline;
document.getElementById("p1").innerHTML =r4;
document.getElementById("p2").innerHTML ="Checks whether the 'm' modifier is
set";
}
function source()
{
var pattern = new RegExp("JavaScript","ig");
var r5=pattern.source;
document.getElementById("p1").innerHTML =r5;
document.getElementById("p2").innerHTML ="Returns the text of the RegExp
pattern";
}
</script>
</body>
</html>
Frames
HTML Frames are used to divide the 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 frameset.
The window is divided into frames in a similar way the tables are organized into rows
and columns.
<Frame> Object
It is an element that represents the html frame which defines one particular window
(frame) within a frameset.
It defines the set of frames that make up the browser window.
It is a property of the window object.
It has no end tag but needs to be closed properly.
A <frame> should be used within a <frameset> tag only, since it defines a particular
area in which another HTML document can be displayed.
The attributes of the <frame> element are as follows:
Attribute Description
src It is used to give the filename that should be loaded in the frame. Itsvalue can be any url.
name It is the name of the frame, which is used to indicate that a document should be loaded into
a frame.
frameborder It specifies whether or not the borders of that frame are shown. Possible values are 0 or 1.
marginwidth It specifies the space between the left and right of the frame’s border and content. The value
is given in pixels.
marginheight It specifies the space between the top and bottom of the frame’s border and content. The
value is given in pixels.
noresize It prevents the user from being able to resize the frame by dragging the frame borders.
scrolling It controls the appearance of scrollbars that appear on the frame. Possible values are yes,
no and auto.
longdesc It allows to provide a link to another page which contains a long description of the contents
of the frame.
<Frameset> Object
Frameset object holds two or more frame elements and each frame element in turn holds
a separate document.
This object states only how many columns or rows there will be in the frameset.
Syntax:
<frameset> . . . </frameset>
The attribute of the <frameset> are as follows:
Attribute Description
cols It specifies how many columns are to be contained in the frameset and the size of each column.
rows It works like the ‘cols’ and takes the same values, but is used to specify the number of rows in the
frameset.
border It specifies the width of the border of each frame in pixels.
frameborder It specifies whether a 3D border should be displayed within frames. Possible values are 0 or 1.
bordercolor It sets the color of the border between frames.
framespacing It specifies the amount of space between frames in a frameset. It can accept any integer value.
Creating a frame
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 frames.
Following is the example to create three horizontal frames –
frame.html
<html>
<head>
<title>HTML Frames</title>
</head>
<frameset rows = "30%, 40%, * ">
<frame name = "top" src = "frame1.html" />
<frame name = "main" src = "frame2.html" />
<frame name = "bottom" src = "frame3.html" />
</frameset>
</html>
frame1.html
<html>
<head>
<title>HTML Frames</title>
</head>
<body>
<h2>This is Top frame </h2>
</body>
</html>
frame2.html
<html>
<head>
<title>HTML Frames</title>
</head>
<body>
<h2>This is Main frame </h2>
</body>
</html>
frame3.html
<html>
<head>
<title>HTML Frames</title>
</head>
<body>
<h2>This is Bottom frame </h2>
</body>
</html>
<html>
<head>
<title>HTML Frames</title>
</head>
<frameset rows = "30%,70%" >
<frame name = "top" src = "frame1.html" frameborder="0" border="0"/>
<frameset cols = "*, *">
<frame name = "main" src = "frame2.html" frameborder="0" border="0"/>
<frame name = "bottom" src = "frame3.html" frameborder="0" border="0"/>
</frameset>
</frameset>
</html>
main_frame.html
<html>
<frameset cols="25%,75%">
<frame name="frame1" src="frame1.html"></frame>
<frame name="frame2" src="frame2.html"></frame>
</frameset>
</html>
frame1.html
<html>
<body>
<h3> Frame 1</h3>
<input type="button" name="button1" value="click"onclick="parent.frame2.display()"/>
</body>
</html>
frame2.html
<html>
<head>
<script>
function display()
{
document.write("<h3>This function is called from frame1</h3>");
}
</script>
</head>
<body>
<h3> Frame 2 </h3>
</body>
</html>
main.html
<html>
<body>
<h3>This is main page and content from any link will be displayed here.</h3>
<p>So now click any link and see the result.</p>
</body>
</html>
menu.html
<html>
<body>
<a href = "https://www.google.com" target = "main_page">Google</a> <br/><br/>
<a href = "https://www.gmail.com" target="main_page">Gmail</a> <br/><br/>
<a href = "https://www.yahoo.com" target = "main_page">Yahoo</a>
</body>
</html>
<html>
<head>
<title>Parent window</title>
</head>
<frameset cols="*,*">
<frame src="frame1.html" name="frame1">
<frame src="frame2.html" name="frame2">
</frameset>
</html>
<html>
<head>
<title>child window</title>
<script>
function display()
{
parent.frame2.document.write("<h3>This message is written by frame 1</h3>");
}
</script>
</head>
<body>
<h4> Frame 1</h4>
<p>Click below button to write on frame 2</p>
<input type="button" name="button1" value="Click Here" onclick="display()">
</body>
</html>
<html>
<head>
<title>child window</title>
</head>
<body>
<h4> Frame 2</h4>
<p id="demo"> contents of frame 2 </p>
</body>
</html>
Rollover
Rollover is a JavaScript technique used by Web developers to produce an effect in
which the appearance of a graphical image changes when the user rolls the mouse
pointer over it.
Rollover also refers to a button on a Web page that allows interactivity between the
user and the Web page.
Creating Rollover
A rollover is created by making use of ‘onmouseover’ event when the mouse pointer is
moved onto an element or onto one of its children.
The <img> tag is used to define the image object.
The value assigned to the src attribute of the <img> tag identifies the image itself.
Now using onmouseover event you can change the image.
<html>
<head>
<title>Image Rollover</title>
</head>
<body>
<h2>Image Rollover</h2>
<h3>Move cursor over and out of an image to see result</h3>
<img src= “https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcTnLqt7Y1gm8bYfrVNlUZJxSaso1GUy8sU_eQ&usqp=CAU”
onmouseover="src ='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPY-
f6bhIp95dhyMyXRWxvc_Kw-zZow3g5OA&usqp=CAU'" onmouseout = " src = 'https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcRW_K62fzNMBM0o97zU1ZTZ2ho6UsVNQIamN3sz1Rc83N4r_
e6sB1cHw0HmzCHiJA93_hM&usqp=CAUg'">
</body>
</html>
Text Rollover
<html>
<body>
<h2>text rollover</h2>
<a onmouseover="document.fruits.src='https://images.everydayhealth.com/images/apples-101-
about-1440x810.jpg?sfvrsn=f86f2644_1'">
<h3>apple</h3>
</a>
<a onmouseover="document.fruits.src='https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcRAjcii1Eb3k8OWnnBCzOU3kC1AAz9qpYEcgw&usqp=CAU'">
<h3>pineapple</h3>
</a>
<img src="https://webartdevelopers.com/blog/wp-content/uploads/2022/02/rolling-text-hover-
animation.gif" alt="" name="fruits" width="330" height="250">
</body>
<html>
<html>
<body>
<script>
function mouseout()
{
document.getElementById("p1").innerHTML= "This is mouseout event..."
}
document.addEventListener("mouseover", function()
{ document.getElementById("p1").innerHTML="This is mouseover event..."});
document.addEventListener("mouseout", mouseout);
</script>
<h1 id="p1">Rollover using addEventListener... </h1>
</body>
</html>