Javascript Tutorials - Part 1

Programming for Search Engines 101. An area for avid PHP and .NET developers to chat about Programming techniques and how to make better use of search engines.

Moderator: Moderators

Javascript Tutorials - Part 1

Postby jay » Wed May 14, 2008 6:13 am

Javascript Tutorials - is focused on improving the programming skills in javascript and is basically for programmers. Anyone can post or ask questions regarding javascript programming. Let's start with Regular expressions, which is used basically for validations.

1. Using Regular expressions.

Regular expressions are a type of pattern matching. They allow you to easily describe a pattern in text and are a great tool for validating textual data. In addition to pattern matching, they can be used to replace text.JavaScript's regular expressions simplifies data validation.

A basic feature is anchors, which allow you to specify the start and/or end of a string. The caret (^) is used to indicate the beginning of a string, while the dollar sign ($) indicates the end. You should use an escape sequence if you need to include a caret or dollar sign in a search string. The escape character is a backslash (\), and it precedes the caret or dollar sign. The following sample matches the word search when it appears by itself in a string:

^search$

In addition, you can search for a set of characters, which are enclosed in square brackets, that is [ and ]. A character set is a set of characters to which a matched character must belong. A good example is searching for a number in a range like [12345] to match a number from one to five; it may also be written as [1-5].

Many times you will need to specify that a character may appear multiple times, or it is optional. The question mark (?) means the character preceding it is optional. In addition, the plus sign (+) means one or more of the previous characters may appear and the asterisk (*) means zero or more of the previous character may appear.

Working with strings

Every JavaScript string variable includes regular expression support via three methods: match(), replace(), and search(). In addition, the object test() method allows you to test. Here's more information about match(), replace(), and search():

* match(): Used to match a regular expression against a string. If one or more matches are made, an array is returned that contains all of the matches. Each entry in the array is a copy of a string that contains a match. If no match is made, a null is returned.
* replace(): Used to match a regular expression against a string and replace any match with a new substring. The first parameter is the regular expression, and the second parameter is the replace string.
* search(): Used to search for a match between a regular expression and the specified string. If a match is found, the search method returns the index of the regular expression within the string. If no match is found, a value of -1 is returned.

RegExp

The RegExp object contains the pattern of a regular expression. It matches strings using its methods and properties. There are two ways to create an instance of a RegExp object: by using the constructor function, or with a literal text format with the text pattern of the regular expression specified in both cases. It contains an optional second parameter that signals if the search is global (g), ignore case (i), or globally ignore care (gi). This is a simple example of creating a RegExp object using the constructor approach to search for a string while ignoring case:

testRegExp = new RegExp("^search$","I")

You can create the same instance using the literal text format (enclosed in forward slashes):

testRegExp = /^search$/i

TEST IT YOURSELF.

Example 1.

<html><head>
<title>RegExp test</title>
</head><body>
<script language="javascript">
testRegExp = /search/i;
if (testRegExp.test("this is a search string")) {
alert("The string was found.");
} else {
alert("No match found.");
}
</script>
</body>
</html>

Example 2.

<html>
<head>
<title>RegExp validation</title>
<script language="JavaScript">

function validate() {
var doc = document.test;
var valName = new RegExp("^(Tony|Anthony) Patton", "i");

if (doc.Name.value.match(valName) == null) {
alert("Name was not found.");
} else {
doc.Name.value = doc.Name.value.replace(valName, "T. Patton");
}

varvalTime = new RegExp("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$");
if (doc.time.value.match(varvalTime) == null) {
alert("Please enter correct time format (hh:ss)");
}
else
{
alert("Time is correct");
}
}

</script>
</head>
<body><form name="test">
Name: <input type="text" name="Name" value=""><br>
Time: <input type="text" name="time" value=""><br>
<input type="button" name="test" value="test" onClick="validate();">
</form>
</body>
</html>
Jay M
Write Less, Do More
jay
 
Posts: 475
Joined: Wed Nov 22, 2006 12:05 am
Location: Cochin, India.

Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

cron