Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 2:23 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Regular Expressions and Patterns
PostPosted: Mon Aug 17, 2009 1:14 pm 
Offline
Newbie
User avatar

Joined: Fri Jun 26, 2009 5:50 am
Posts: 23
Has thanked: 0 time
Have thanks: 1 time

Hello,
Regular expressions are very powerful tools for performing pattern matches. PERL programmers and UNIX shell programmers
have enjoyed the benefits of regular expressions for years. Once you master the pattern language, most validation tasks become trivial. You can perform complex tasks that once required lengthy procedures with just a few lines of code using regular expressions.

So how are regular expressions implemented in JavaScript? There are two ways:

1) Using literal syntax.
2) When you need to dynamically construct the regular expression, via the RegExp() constructor.

The literal syntax looks something like:

var RegularExpression = /pattern/

while the RegExp() constructor method looks like

var RegularExpression = new RegExp("pattern");

The RegExp() method allows you to dynamically construct the search pattern as a string, and is useful when the pattern is not known ahead of time.

To use regular expressions to validate a string you need to define a pattern string that represents the search criteria, then use a relevant string method to denote the action (ie: search, replace etc). Patterns are defined using string literal characters and metacharacters. For example, the following regular expression determines whether a string contains a valid 5-digit US postal code (for sake or simplicity, other possibilities are not considered):

<script language="JavaScript1.2">
function checkpostal(){
var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
if (document.myform.myinput.value.search(re5digit)==-1) //if match failed
alert("Please enter a valid 5 digit number inside form")
}
</script>

<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="checkpostal()" value="check">

</form>

_________________
Thanks & regards
Lokananth
[url "http://www.mioot.com"]Live Chat Software[/url] By miOOt


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 java regular expression  Java examples  msi_333  0
 I need a book (Patterns for Parallel Programming)  General Discussion  loomy  2
 java Expressions  Java  msi_333  0
 press.Java.Regular.Expressions.Taming.the.java.Dot.util.Dot.  offers  Casper  0
 (O'Reilly) CSharp 3.0 Design Patterns  offers  Casper  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team