0% found this document useful (0 votes)
17 views31 pages

2.4 Regular Expressions

Uploaded by

umathamizh0306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views31 pages

2.4 Regular Expressions

Uploaded by

umathamizh0306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

2.

4 REGULAR
EXPRESSIONS
Regular Expression
 A regular expression is a sequence of characters that
forms a search pattern.
 The search pattern can be used for text search and text
replace operations.
 A regular expression can be a single character, or a
more complicated pattern.
 Regular expressions can be used to perform all types of
text search and text replace operations.
 Syntax
/pattern/modifiers;
 Example
var patt = /w3schools/i;
 Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-
insensitive).
Using String Methods
 In JavaScript, regular expressions are often
used with the two string methods: search() and
replace().
 The search() method uses an expression to
search for a match, and returns the position of
the match.
 The replace() method returns a modified string
where the pattern is replaced.
Using String search() With a
String
 The search() method searches a string for a
specified value and returns the position of the
match:
 Example

 Use a string to do a search for "W3schools" in a


string:
 var str = "Visit W3Schools!";
 var n = str.search("W3Schools");
Using String replace() With
a String
 The replace() method replaces a specified
value with another value in a string:
 var str = "Visit Microsoft!";
var res = str.replace("Microsoft",
"W3Schools");
Use String replace() With a
Regular Expression
Example
Use a case insensitive regular
expression to replace Microsoft
with W3Schools in a string:
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i,
"W3Schools");
Regular Expression
Modifiers
Modifiers can be used to
perform case-insensitive more
global searches:
Regular Expression
Patterns
 Bracketsare used to find a range of characters
 Expression Description

 [abc] Find any of the


characters between the brackets
 [0-9] Find any of the digits
between the brackets
 (x|y) Find any of the
alternatives separated with
Using the RegExp
Object
 In JavaScript, the RegExp object is a regular expression
object with predefined properties and methods
 Using test()
 The test() method is a RegExp expression method.
 It searches a string for a pattern, and returns true or
false, depending on the result.
 sUsing exec()
 The exec() method is a RegExp expression method.
 It searches a string for a specified pattern, and returns
the found text as an object.
 If no match is found, it returns an empty (null) object.
2.5 EXCEPTION HANDLING
EXCEPTION HANDLING
There are three types of errors in
programming:
 Syntax Errors
 Runtime Errors, and
 Logical Errors.
Syntax Errors
 Syntax errors, also called parsing errors, occur
at compile time in traditional programming
languages and at interpret time in JavaScript.
 <script type = "text/javascript">
 <!--

 window.print(;
 //-->
 </script>
Runtime Errors
 Runtime errors, also called exceptions, occur
during execution (after
compilation/interpretation).
 <script type = "text/javascript">
 <!--

 window.printme();
 //-->
 </script>
Logical Errors
 Logic errors can be the most difficult type of
errors to track down.
 These errors are not the result of a syntax or
runtime error. Instead, they occur when you
make a mistake in the logic that drives your
script and you do not get the result you
expected.
The try...catch...finally
Statement
 The latest versions of JavaScript added exception handling
capabilities.
 JavaScript implements the try...catch...finally construct as well

as the throw operator to handle exceptions.


<script type = "text/javascript">
<!--
try {
// Code to run [break;]
}
catch ( e ) {
// Code to run if an exception occurs [break;]
}
[ finally {
// Code that is always executed regardless of an
exception occurring
}]
Example
<html>
<head>
<script type = "text/javascript">
<!--
function myFunc() {
var a = 100;
alert("Value of variable a is : " + a );
}
//-->
</script>
</head>

<body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
Example
<html>
<head>

<script type = "text/javascript">


<!--
function myFunc() {
var a = 100;
try {
alert("Value of variable a is : " + a );
}
catch ( e ) {
alert("Error: " + e.description );
}
}
//-->
</script>

</head>
<body>
<p>Click the following to see the result:</p>

<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>

</body>
Example
<html>
<head>

<script type = "text/javascript">


<!--
function myFunc() {
var a = 100;

try {
alert("Value of variable a is : " + a );
}
catch ( e ) {
alert("Error: " + e.description );
}
finally {
alert("Finally block will always execute!" );
}
}
//-->
</script>

</head>
<body>
<p>Click the following to see the result:</p>

<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>

</body>
The throw Statement
Use throw statement to raise the
built-in exceptions or
thecustomized exceptions
Example
<html> <head>
<script type = "text/javascript">
<!--
function myFunc() {
var a = 100; var b = 0;
try {
if ( b == 0 ) {
throw( "Divide by zero error." );
}
else {
var c = a / b; }
}
catch ( e )
{ alert("Error: " + e ); }
}
//--> </script> </head> <body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form> </body> </html>
The onerror() Method
 The onerror event handler was the first feature
to facilitate error handling in JavaScript.
 The error event is fired on the window object
whenever an exception occurs on the page.
Example
<html>
<head>
<script type = "text/javascript">
window.onerror = function ()
{
alert("An error occurred.");
}
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick =
"myFunc();" />
</form>
</body>
</html>
2.6 VALIDATION
Form validation
 Form validation normally used to occur at the server,
after the client had entered all the necessary data and
then pressed the Submit button.
 This was really a lengthy process.
 JavaScript provides a way to validate form's data on the
client's computer before sending it to the web server.
 Form validation performs two functions.
 Basic Validation − form must be checked to make sure
all the mandatory fields are filled in.
 Data Format Validation - data that is entered must be
checked for correct form and value.
2.7 BUILT IN OBJECT
BUILT IN OBJECTS
 Booleans can be objects (if defined with the new
keyword)
 Numbers can be objects (if defined with the new
keyword)
 Strings can be objects (if defined with the new keyword)
 Dates are always objects
 Maths are always objects
 Regular expressions are always objects
 Arrays are always objects
 Functions are always objects
 Objects are always objects
JavaScript Primitives
A primitive value is a value that has no
properties or methods.
A primitive data type is data that has a
primitive value.
 JavaScript defines 5 types of primitive data
types:
String
Number
Boolean
Objects are Variables
 JavaScript variables can contain single values
 Example
var person = "bala";
 Objects are variables too. But objects can
contain many values
 The values are written as name : value pairs
(name and value separated by a colon).
 var person = {firstName:“Bala",
lastName:“Kumar", age:35};
Object Properties
The named values, in JavaScript
objects, are called properties.
Object Methods
 Methods are actions that can be performed on
objects.
 Object properties can be both primitive
values, other objects, and functions.
 An object method is an object property
containing a function definition.
Creating a JavaScript Object
 With JavaScript, we can define and create the own objects.
 There are different ways to create new objects:
 Define and create a single object, using an object literal.
var person = {firstName:“Bala", lastName:“Kumar", age:35};
 Define and create a single object, with the keyword new.
var person = new Object();
 Define an object constructor, and then create objects of the
constructed type.
var person = {firstName:“Bala", lastName:“Kumar", age:35};
var x=person;
x.age = 10; // This will change both x.age and person.age

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy