Skip to main content

Regular expressions in JavaScript

JavaScript includes full support for Perl-style regular expressions, and it's extremely useful for string matching processes.



Coding a script to check sensitive user data is sometimes pretty straightforward. But most of the time this process is not so easy. With current websites, we'll
need to check the proper standardized format of an email address or a URL. That would be a nightmare for programmers, not to mention a confusing and inefficient error-prone process for checking data validity.



A regular expression is a way of describing a pattern in a piece of text. In fact, it's an easy way of matching a string to a pattern. We could write a simple regular expression and use it to check, quickly, whether or not any given string is a properly formatted user input.




Here are a few special characters that can be used for matching characters in regular expressions:


\n // a newline character


. // any character except a newline


\r // a carriage return character


\t // a tab character


\b // a word boundary (the start or end of a word)


\B // anything but a word boundary.


\d // any digit (same as [0-9])


\D // anything but a digit (same as [^0-9])


\s // single whitespace (space, tab, newline, etc.)


\S // single nonwhitespace.


\w // A ?Ąăword character?? (same as [0-9a-zA-Z_])


\W // A ?Ąănonword character (same as [^0-9a-zA-Z_]


match()

Verify Phone number (123) 456-7890






validateForm = function() {

if ( checkPhone( this.phone, 'Please enter a valid phone number') ) {

return true;

}

return false;

}

checkPhone= function( field, errorMsg) {

phoneRegex = /^\(\d{3]\) \d{3}-\d{4}$/;

if( !field.match( phoneRegex ) ) {

alert( errorMsg );

field.focus();

field.select();

return false;

}

return true;

}


replace()


function formatField( fieldValue ) {

return fieldValue = fieldValue. replace(/\n/g, Ą°<br />Ąą);

}


search()


pos = htmlString.search(/^<a href="http:\/\/$/i);

if ( pos != -1) {

alert( 'First absolute link found at ' + pos +'position.');

}

else {

alert ( 'Absolute links not found');

}



test()




The test() method is somewhat particular and different from the rest, as we'll
see shortly. Within the JavaScript context, when a pattern is defined following
the syntax previously described, we are actually defining a new object, called
a ?Ąăregular expression object??. All we need to know is that this object owns
the proprietary test() method, which allows us to perform string matching according
to a given string.


The test() method takes a given string as a parameter and looks for matches
according to the pattern defined within the regular expression object itself.
If any matches are found, it will return true. If no matches are found, then
it will return false. Let's see an example to explain how this method works:


function validateEmail ( emailField, errorMsg ) {

emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;

if( !emailpat.test( emailField.value ) ) {

alert( errorMsg);

emailField.focus();

emailField.select();

return false;

}

return true;

}


To validate an email address, we should call the function as:


validateEmail( this.email , 'Please enter a valid email address');




Comments

Popular posts from this blog

Android Developer Learning Resource

Developing In Eclipse, with ADT http://developer.android.com/guide/developing/eclipse-adt.html Signing Your Applications http://developer.android.com/guide/publishing/app-signing.html Compile and sign with Eclipse ADT To create a signed and aligned .apk in Eclipse: Select the project in the Package Explorer and select File > Export . Open the Android folder, select Export Android Application, and click Next . The Export Android Application wizard now starts, which will guide you through the process of signing your application, including steps for selecting the private key with which to sign the .apk (or creating a new keystore and private key). Complete the Export Wizard and your application will be compiled , signed , aligned , and ready for distribution. DroidDraw is a graphical user interface (GUI) builder for the Android platform http://code.google.com/p/droiddraw/

RMO programming

http://msdn2.microsoft.com/en-us/library/ms151819.aspx SQL Server Express does not include SQL Server Agent, which is typically used to run replication agents. If you use a pull subscription (in which agents run at the Subscriber), you must synchronize the subscription using Windows Synchronization Manager or RMO. For more information, see the following topics: Windows Synchronization Manager RMO programming: How to: Synchronize a Pull Subscription (RMO Programming) Replication does not support the Run As Normal User (RANU) feature of SQL Server Express. For more information about this feature, see the SQL Server Express documentation.