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


    lunes, 14 de abril de 2014

    ASP.Net MVC 3.0 with Microsoft Visual Web Developer 2010 Express (Part 1)


    Hi, this is my first tutorial of 2014 , and i want to start with one of my favorite topic : MVC & ASP.Net

    Points of this tutorial :
    • Fast (really very fast) Introduction to MVC 
    • Show me the code !!! 
    • Conclusions

    Ok.. Here we go !!



    Fast (really very fast) Introduction to MVC


    MVC = Model, View and Controller


    The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly.


    The MVC framework includes the following components:


    Models. Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database.


    In small applications, the model is often a conceptual separation instead of a physical one. For example, if the application only reads a dataset and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the dataset takes on the role of a model object.


    Views. Views are the components that display the application's user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Product object.


    Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn might use these values to query the database.


    All this translates to the following : Independence of different layers , parallel development , Easy implementation of Unit Test, and business complexity management.





    Show me the code !! (Hello World ASP.Net MVC 3.0)


    Ok,  it´s time to Code and Coffee :)

    For this tutorial we need :

    Let´s start :
    • Run Visual Web Developer 2010 Express
    • Select File option from top menu, and then select New Project option



    • "New Project" window appear

      1. Select from Installed Templates, "Visual C#"  and then select "Web" option.
      2. From the right list , select  "ASP.Net MVC 3 Web Application"  template.
      3. Set project Name ("MvcHelloWorld" for this tutorial)
      4. Set project Location.
      5. Set Solution Name ("MvcHelloWorld" for this tutorial)
      6. Check "Create directory for this solution" option
      7. Click "OK" button.


    • The New ASP.NET MVC 3 Project dialog box appear





      1. select Internet Application
      2. leave Razor as the default view engine.
      3. Click "OK" button.


    • Default template for the ASP.NET MVC project has been created:





    • From the Debug menu, select Start Debugging.



    • Yeii!!!  Your first MVC 3.0 Web Application.