Translate

Saturday, February 8, 2014

Evaluate Regular Expression using Javascript

 Welcome to FullStackTutorials, in this post, I will teach you how to evaluate Regular Expression (REGEX ) using Javascript.

To evaluate Regular Expression, we need to use the Javascript function called "TEST",this function will help us to assess a pattern, so if the pattern matches with the string will return true otherwise false.


Example:

we are define 2 variable:
  • The pattern 
  • The string to be assessed



//as we can see this pattern is to validate emails

var Pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
 


.

//This is the string to be evaluated
var mystring = 'myemail01@mydomin.com';
 


To use the test" function  is so easy as this, first we need put the pattern variable followed by a dot(.) then we put the function test and finally we put our string inside the parentheses.


.

var result = Pattern.test(mystring);

document.write('Patron: '+Pattern+' '+'
My Email: '+mystring);

alert(result);
 



Output:





The whole code is:





1 comment: