JavaScript - Regular Expressions
JavaScript - Regular Expressions
JavaScript - Regular Expressions
Regular Expressions
B.Bhuvaneswaran
Assistant Professor (SS)
Department of Computer Science & Engineering
Rajalakshmi Engineering College
Thandalam
Chennai 602 105
bhuvaneswaran@rajalakshmi.edu.in
Regular Expressions
A regular expression is a sequence of
characters that forms a search pattern.
The search pattern can be used for text
search and text replace operations.
Syntax
/pattern/modifiers;
Example:
Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a
search).
i is a modifier (modifies the search to be
case-insensitive).
Using test()
The test() method is a RegExp expression
method.
It searches a string for a pattern, and
returns true or false, depending on the
result.
Example
The following example searches a string
for the character "e":
var patt = /e/;
patt.test("The best things in life are
free!");
Since there is an "e" in the string, the
output of the code above will be:
true
Using exec()
The exec() method is a RegExp expression
method.
It searches a string for a specified pattern,
and returns the found text.
If no match is found, it returns null.
Example
The following example searches a string
for the character "e":
/e/.exec("The best things in life are
free!");
Since there is an "e" in the string, the
output of the code above will be:
e
References
http://www.w3schools.com/