Search within TutorialsPark

Javascript Regular Expressions: Introduction

An Intro to regular expressions, its application and limitations .

Javascript Regular Expressions: What is Regular Expression?

A regular expression is an object that decribes a specific pattern of characters, it is matched against a string to see if these is successful match.

JavaScript Regular Expressions are used to check input data on the client side. It can be used to verify form inputs like e-mail address, password, Credit Card No etc.

Just like JavaScript, a regular expression object is created using two ways:

1. By assigning a literal expression to a variable.

2. Using a RegExp constructor to create and return a regular expression.

Regular expression using : Literals Notations

A regular expression object using literals is created by assigning a regular expression to a variable.

If you see a regular expression, its a pattern of characters enclosed within forward slashes. flags like i, g and m are used after the forward slash inorder to modify the search pattern.

Table: Regular Expression Flags to Modify the Search Pattern.

Characters Description
i To perform a case-insensitive matching
g To perform a global match, i.e to find all matches instead of stopping after the first match.
m Multiline Mode. To match beginning of a line or beginning of a string, and $ is used to match end of a line or end of a string.

Format and Example: Flags for Modififying Search Patterns.

Javascript Regular Expression: Using Constructor Method.

A RegExp object is created using the JavaScript Constructor method RegExp(). It can take two arguments : regular expression pattern and Option flag.

The Contructor method is used when the regular expression can change throughout the run of the program, if the regular expression is not going to change(i.e hard coded) then the literal notation method is faster.

Example and Format: JavaScript Regular Expression - Constructor Method.

JavaScript Regular Expressions: Testing Expressions.

Inorder to test for a match within an input string, the RegExp object has two methods : the test() method and the exec() method.

Table: Methods of RegExp Object.

Method Description
exec() Searches the string for a match, if a match is found it returns the results in an array, or else a null is returned.
test() It takes a string and returns true if the string contains a match for the regular expression, or else a false is returned.

JavaScript Expressions: test() Method .

The RegExp object's test() method is used to check if the input string matches with the pattern specified by the regular expression.

The RegExp object property lastIndex specifies the position(starting from 0) in the string where the next search would begin.

Incase of a global search(using flag g) the lastIndex property specifies the starting position after the last pattern matched.

Syntax: JavaScript Regular Expressions: test() method.

Example: JavaScript Regular Expression: test() method using Literal Notations

Give it a TRY! » Note: Regular expressions are case-sensitive, hence sparta instead of Sparta will not match.

JavaScript Regular Expressions: method test() Using RegExp Constructor.

Give it a TRY! » Note:Constructor RegExp() creates a regular expression object regular and assigned the string Sparta.

JavaScript Regular Expressions: Method exec().

The method exec() is used to excute a regular expression on the specified string, it searches the string for a match, if nothing is found it returns a null. But if a match is found the result is returned in an array.

Syntax: JavaScript Regular Expression exec() Method.

Example: JavaScript Regular Expression exec() Method.

Give it a TRY! » Note:If the method does not return a null value, then a match must have been found.