martes, 13 de mayo de 2014

Java Script : Regular Expressions Overview

Definition

In JavaScript , a regular expression is an object that describes a pattern of characters.

Commonly are used in functions for search o replace specific text , or pattern-matching (values validate)


Syntax

A regular expression basically is formed by : Pattern and Modifier

Pattern : specifies the pattern of an expression
Modifier: specify if a search should be global, case-sensitive, etc.

Modifiers can be:

ModifierDescription
iPerform case-insensitive matching
gPerform a global match (find all matches rather than stopping after the first match)
mPerform multiline matchin



You can write a regular expression in Javascript in two modes:

use RegExp class : 

var patt=new RegExp(pattern,modifiers);

or more simply (used for all examples in this article):

var patt=/pattern/modifiers;

Example:

  • Do global search of "hello" word: 

    var regExp = /hello/gi;
    Where "hello" is a pattern and "g" and "i" are modifiers

Complete implementation can be:

function myFunction()
{
var str = "Hello , some body in home ? ";
var regExp = /hello/ig;
var result = str.match(regExp);
alert(result);
}


A regular expression pattern can be conformed by :

Brackets

Brackets are used to find a range of characters

Example  :



  • Do global (modifier "g") search of number between 1 an 4 :
    • var regExp = /[1-4]/g;
    • Example: From value "9043192567" , the match is "43125"

      Do global (modifier "g") search of any character from lowercase "a" to lowercase "z":
      • var regExp = /[a-z]/g;
      • Example: From value "HELLO kitty", the match value is "kitty"

    Metacharacters

    Metacharacters are characters with special meaning

    Example:

    • Find a white space character:
      • var regExp = /\s/g;
    • Find a digit :
      • var regExp = /\d/g;

    Quantifiers

    Quantifiers match the preceding subpattern a certain number of times. The subpattern can be a single character, an escape sequence, a pattern enclosed by parentheses or a character set.

    Example :
    • Match exactly all value in some text where "o" character appear 2 times :
      • var regExp = /o{2}/g
      • Example value :  "tooth and nose" , the match value is "oo" from "tooth" word
    • Matches any digit that is not followed by "-" character  :
      • var regExp = /\d(?!-)/g
      • Example value :  "01-800-123-456" , the match value is 0,8,0,1,2,456

    RegExp Object Method

    In Javascript ,  RegExp object provides  following methods :

    • exec() : Tests for a match in a string. Returns the first match

      1. function myFunction() {
      2.     var str = "The best things in life are free";
      3.     var patt = new RegExp("e");
      4.     var res = patt.exec(str);
      5.     alert(res);
      6. }
      • The result of  res will be : "e"
    • test() : Tests for a match in a string. Returns true or false


      1. function myFunction() {
      2.     var str = "The best things in life are free";
      3.     var patt = new RegExp("e");
      4.     var res = patt.exec(str);
      5.     alert(res);
      6. }
      • The result of  res will be : true
    • tostring(): Returns the string value of the regular expression

      1. function myFunction() {
      2.     var patt = new RegExp("Hello World", "g");
      3.     var res = patt.toString();
      4.     alert(res);
      5. }
      • The result of  res will be : "/Hello World/g"


    Common Examples:


    • Email Validation: Boolean function it return true if e-mail is a valid  adress, return false if isn´t.
      1. function emailValidation(email)
      2. {
      3.  var regExp = new RegExp("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$","g");
      4.  alert(regExp.test(email));
      5. }



    • Print Pages Validation: Boolean function it return true if received value match with the example values : 1,2,3-4,1 (like when you send to print pages)
      1. function printPagesValidation(pages)
      2. {
      3.  var regExp = new RegExp("^([\\d]+)$|([\\d]+)[-,]([\\d]+)$","g");
      4.  alert(regExp.test(pages));
      5. }

    References :

    JavaScript RegExp Object : http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    Regular Expressions patterns: http://www.javascriptkit.com/javatutors/redev2.shtml


    Publicar un comentario en la entrada