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:
Modifier | Description |
---|---|
i | Perform case-insensitive matching |
g | Perform a global match (find all matches rather than stopping after the first match) |
m | Perform 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;
- Do global search of "hello" word:
- Where "hello" is a pattern and "g" and "i" are modifiers
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 :
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
- function myFunction() {
- var str = "The best things in life are free";
- var patt = new RegExp("e");
- var res = patt.exec(str);
- alert(res);
- }
- The result of res will be : "e"
- test() : Tests for a match in a string. Returns true or false
- function myFunction() {
- var str = "The best things in life are free";
- var patt = new RegExp("e");
- var res = patt.exec(str);
- alert(res);
- }
- The result of res will be : true
- tostring(): Returns the string value of the regular expression
- function myFunction() {
- var patt = new RegExp("Hello World", "g");
- var res = patt.toString();
- alert(res);
- }
- 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.
- function emailValidation(email)
- {
- var regExp = new RegExp("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$","g");
- alert(regExp.test(email));
- }
- 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)
- function printPagesValidation(pages)
- {
- var regExp = new RegExp("^([\\d]+)$|([\\d]+)[-,]([\\d]+)$","g");
- alert(regExp.test(pages));
- }
References :
JavaScript RegExp Object : http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Regular Expressions patterns: http://www.javascriptkit.com/javatutors/redev2.shtml
Regular Expressions patterns: http://www.javascriptkit.com/javatutors/redev2.shtml