2.4 Regular Expressions
2.4 Regular Expressions
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
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
<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>
</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>
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