Aptech - JavaScript Session - 2
Aptech - JavaScript Session - 2
nl
O
se
U
tre
Session 2
en
C
Matching Patterns in JavaScript
h
ec
pt
rA
Fo
Objectives
y
nl
O
By the end of this session, students will learn to:
se
Explain Regular Expressions in JavaScript and its uses
U
Identify RegExp object and matching patterns in JavaScript
tre
Describe the uses of modifiers, brackets and Metacharacters
en
Describe properties and methods of RegExp in JavaScript
C
h
ec
pt
rA
Fo
© Aptech Limited
Overview of Regular Expressions
y
nl
O
Regular Expressions (RegExp):
se
Used to search specific data in a text based on a pattern.
U
Consists of pattern and optionally flags.
tre
Benefits are:
en
Formulates RegExp
C
with a special
Used to find Retrieves all the syntax where
h
specific data from a search results in developers can
code, spreadsheets,
log files, and ec
one go, minimizing
effort and time.
search text, replace
substring, and
pt
documents. extract information
from a string.
rA
Fo
© Aptech Limited
RegExp Objects: Pattern and Matcher
y
nl
O
There are two classes for creating RegExp Objects: Pattern and Matcher.
se
U
• Is a keyword that needs to be searched in a
document or code.
tre
Pattern • Can be a string of characters or just one
en
character.
• Has no constructors.
C
h
• Is used after creating a Pattern.
Matcher ec
• Is used to match the pattern against a
sequence of characters.
pt
• Is created by the matcher () method.
rA
Fo
© Aptech Limited
RegExp Objects: Creation
y
nl
O
se
Two ways to define RegExp are:
U
1. Instantiating a new RegExp object using the constructor.
tre
2. Using the RegExp Literal Form.
en
C
h
ec
pt
rA
Fo
© Aptech Limited
RegExp Objects
y
nl
O
Object Literal: A string that has a 'name: value' separated by a comma and
se
enclosed in curly brackets.
U
Methods: Methods match, extract, and replace a string.
tre
Reg Flags: Additional flags specified to control the use of Patterns.
en
RegExp Flags Description
C
g Searches until method returns null.
h
i
ec
Performs case in-sensitive search.
pt
m Uses ^ and $ to match the beginning and end of each line.
rA
© Aptech Limited
Modifiers
y
nl
O
Modifiers perform case insensitive and global searches.
se
U
i
tre
en
C
Modifier Types
h
ec
pt
g m
rA
Fo
© Aptech Limited
Brackets
y
nl
O
Brackets are used to find a variety of characters.
se
U
tre
[abc]
en
[^abc]
C
h
[0-9]
ec
pt
[^0-9]
rA
Fo
(x|y)
© Aptech Limited
Metacharacters
y
nl
O
se
Metacharacters are characters that have a special meaning in RegExp.
U
tre
Example of Metacharacters:
en
. \s \n \xxx
C
\w \S \f \xdd
h
\W \b \t \uxxx
\d
\D
\B
\0
\w
\vec
pt
rA
Fo
© Aptech Limited
Quantifiers
y
nl
Quantifiers help to specify the number of occurrences of a particular pattern.
O
se
n+
U
?!n n*
tre
?=n n?
en
Quantifiers
C
h
^n n{X}
ec
pt
n$ n{X,Y}
rA
n{X,}
Fo
© Aptech Limited
RegExp Object: Properties
y
nl
O
Name Description Version
se
constructor Returns the function that creates the RegExp objects’ JavaScript 1.1
prototype.
U
Global Determines to test with regular expression. JavaScript 1.2
tre
ignoreCase Specifies the case is to be ignored during pattern JavaScript 1.2
matching in a string.
en
Input String against which a regular expression is matched. JavaScript 1.2
lastIndex Specifies the starting index for the next match. JavaScript 1.2
C
lastMatch Indicates the last matched characters. JavaScript 1.2
h
Multiline Specifies if a multiline search is required. JavaScript 1.2
Prototype
ec
Adds new properties and methods to all instances of a
class.
JavaScript 1.1
pt
rightContext Substring following the most recent match. JavaScript 1.2
rA
© Aptech Limited
RegExp Object: Methods
y
nl
O
se
Name Description Version
U
Compile Executes the search for matching a specified JavaScript 1.2
tre
string
Exec Executes a search for matching its string JavaScript 1.2
en
parameter
Test Implements a search for a match between a JavaScript 1.2
C
regular expression and a specified string
h
toSource Used to get a string representation of the JavaScript 1.3
toString
object
ec
Represents the source code of the specified JavaScript 1.1
pt
object
rA
Fo
© Aptech Limited
Summary
y
nl
O
Regular Expression or RegExp is used for searching specific data in a text.
se
The RegExp object has predefined properties. There are two classes involved
in creating a RegExp object: Pattern and Matcher.
U
The RegExp has methods to match, extract, and replace a string.
tre
RegExp Flags match against other strings and additional flags can be specified
en
to control the use of Patterns.
C
Modifiers are used to perform case insensitive and global searches in RegExp.
Brackets are used to find a range of characters.
h
ec
Metacharacters are characters with a special meaning in RegExp.
pt
Quantifiers help to specify number of occurrences of a particular pattern.
rA
Fo
© Aptech Limited