UNIT-V Regular Expression, Rollover and Frames
UNIT-V Regular Expression, Rollover and Frames
UNIT-V Regular Expression, Rollover and Frames
VJTECH ACADEMY
Client Side Scripting ( JavaScript )
Prepared As Per MSBTE I Schema Syllabus
Author:
“Prof. Vishal Jadhav”
[ BE in Computer Engineering and Having 10 years of IT industry experience (Worked in
Capgemini, Amdocs & currently working in TIBCO Software) ]
Regular Expression :
2. Pattern :
The pattern is enclosed between two forward slashes.
The pattern can consist of one or more literals, operators, or constructs.
For example, /abc/ is a regular expression pattern.
The pattern /abc/ matches character combinations in strings only when exactly the
characters 'abc' occur together and in that order.
3. Flags :
Flags are optional parameters that are used to perform case insensitive and global
searches.
Flags are specified after the closing slash of the regular expression.
There are three flags in JavaScript:
- g: global match
- i: case-insensitive match
- m: multiline match
- Example with flags:
- /pattern/g : Searches for all occurrences of pattern in the text.
- /word/i : Matches "word" regardless of case (e.g., "Word," "WORD," "word").
Special
Description
Char/Symbols
1. “test(string)” method :
- This method checks if a string contains a match for the regular expression pattern.
- It returns ‘true’ if a match is found and ‘false’ otherwise.
- Example :
<script language="javascript" type="text/javascript">
var pattern = /apple/;
var text = "I love apples.";
alert(“Your Result Is : ”+pattern.test(text)); // Output: true
</script>
2. “exec(string)” method :
- exec() searches for a match in the given string and returns an array containing
information about the match.
- If no match is found, it returns null.
- The returned array includes the matched text, index, and input string.
- Example :
<script language="javascript" type="text/javascript">
var pattern = /apple/;
var text = "I love apples and apples are delicious.";
var result = pattern.exec(text);
document.write(result);
// Output: ["apple", index: 7, input: "I love apples and apples are
delicious."]
</script>
3. “match(string)” method :
- This method searches for all matches of a regular expression pattern in the given string.
- It returns an array containing all the matched substrings.
- Example :
<script language="javascript" type="text/javascript">
var pattern = /apple/g;
var text = "I love apples and apples are delicious.";
var result = text.match(pattern);
document.write(result); // Output: ["apple", "apple"]
</script>
4. “search(string)” method :
- search() searches for a match in the given string and returns the index of the first match.
If no match is found, it returns -1.
- Example :
<script language="javascript" type="text/javascript">
var pattern = /apples/;
var text = "I love apples and apples are delicious.";
var result = text.search(pattern);
alert("First index of apples: " + result);
</script>
Output
<html>
<body>
Enter a string: <input type="text" id="str" />
<input type="button" value="Find" onclick="find()" />
<script language="javascript" type="text/javascript">
function find() {
var str = document.getElementById("str").value;
var pattern = /[0-9]/gmi;
Output
3. Matching Word
<html>
<body>
Enter Text:<input type="text" id="tf1">
<input type="button" name="b1" value="Check" onclick="CheckOperation()">
<script language="javascript" type="text/javascript">
function CheckOperation() {
var regex = /\w{2}/gmi;
var str = document.getElementById("tf1").value;
var result = regex.test(str);
alert("Regular Expression Result:" + result);
}
</script>
</body>
</html>
Output
4. Matching Characters
<html>
<body>
Enter a string: <input type="text" id="str" />
<input type="button" value="Find" onclick="find()" />
<script language="javascript" type="text/javascript">
function find() {
var str = document.getElementById("str").value;
var pattern = /[abc]/gmi;
// matches any of the characters a, b or c
var result = pattern.test(str);
// returns true if any matching character is found
alert("Matching Character: " + result);
}
</script>
</body>
</html>
Output
Output
6. Email Validation :
<html>
<head>
<title>Email Validation</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function CheckOperation()
{
var regex=/^\w+([-.]?\w+)*@\w+([-.]?\w+)*.\w+([-.]\w+)*$/;
var str=document.getElementById("tf1").value;
var result=regex.test(str);
alert("Regular Expression Result: "+result);
}
</script>
Enter Text:<input type="text" id="tf1">
<input type="button" name="b1" value="Check" onclick="CheckOperation()">
</body>
</html>
7. Mobile No Validation :
<html>
<body>
Enter Your Mobile No :<input type="text" id="tf1">
<input type="button" name="b1" value="Check" onclick="CheckOperation()">
<script language="javascript" type="text/javascript">
function CheckOperation() {
var regex = /^[0-9]{10}$/;
var str = document.getElementById("tf1").value;
var result = regex.test(str);
if (result == true) {
alert("Valid Mobile No");
} else {
alert("Invalid Mobile No");
}
}
</script>
</body>
</html>
Frames :
In HTML and JavaScript, frames are a way to divide a web page into multiple independent
sections, each of which can display a separate HTML document or web page. Frames were
widely used in early web development but have become less common due to various limitations
and compatibility issues. Instead, modern web development often relies on other techniques like
CSS layouts and iframes. However, it's still useful to understand how frames work in HTML
and JavaScript.
1. Creating Frame
- To create frames in HTML, you use the <frameset> element. Inside the <frameset>, you
define individual frames using the <frame> or <iframe> elements. The <frameset> element
is used to define the layout of frames on a web page.
- Example :
<html>
<head>
<title>Main Frame</title>
</head>
<frameset rows="50%, 50%">
<frame src="frame1.html">
<frame src="frame2.html">
</frameset>
</html>
2. <frame>: Within a <frameset>, you use <frame> tags to define individual frames. Each
<frame> tag specifies the source document (src) for the frame.
- Example:
<frameset rows="50%, 50%">
<frame src="frame1.html">
<frame src="frame2.html">
</frameset>
- In this example, the web page is divided into two frames, each loading a separate HTML
document ( frame1.html and frame2.html ).
Frame Examples
Output :
Output :
Output :
Rollover :
Output :
}
function display2() {
document.vjtech.src = "PineApple.jpg";
document.getElementById("p1").innerHTML = "PineApple Image";
}
</script>
</head>
<body>
<h1> Image RollOver </h1><br>
<h2>
<span>--|</span>
<a onmouseover="display1()">Apple</a>
<span>|</span>
<a onmouseover="display2()">PineApple</a>
<span>|--</span>
</h2><br><br>
<a><img src="vjtech.png" name="vjtech"></a>
<h3 id="p1">JavaScript Banner displayed</h3>
</body>
</html>
<script>
function showBook1(name) {
alert("This is : "+name+" Book !!!");
}
function showBook2(name) {
alert("This is : "+name+" Book !!!");
}
</script>
</body>
</html>
VJTECH ACADEMY
ClAss noTEs