The RegExp Object
It is used to validate a pattern
of characters.
How to define a regular expression
1.
Using RegExp objects constructor function.
2.
Using literal.
Using RegExp: var regularexpression = new RegExp(“pattern”,”flag”);
Using literal: /pattern/flag
Types of flags in JavaScript
1. g-
finds all matches globally
2. i-
ignores the case in searches, i.e. text written in lowercase is same as
uppercase.
3. m-
represents multiline matching, i.e. the symbol caret(^) and dollar($) match the
beginning and end of the line as well as
entire string.
Patterns used in the RegExp object
Pattern delimiters:
Pattern
|
Description
|
(pattern),(?:pattern)
|
Matches the entire contained pattern
|
|
(pattern) retains the match
|
|
(?:pattern) does not retain the match
|
Lookaheads
Pattern
|
description
|
(?=pattern),(?!pattern)
|
(?=pattern) matches only if the condition is satisfied
|
|
(?!pattern) matches only if the condition is not satisfied
|
Character Escapes
Pattern
|
Description
|
\f
|
Matches form feed
|
\r
|
Matches carriage return
|
\n
|
Matches linefeed
|
\t
|
Matches horizontal tab
|
\v
|
Matches vertical tab
|
\0
|
Matches null character
|
[\b]
|
Matches back space
|
\s
|
Matches whitespace
|
\S
|
Matches anything but a whitespace
|
\w
|
Matches any alphanumerical character including underscore
|
\W
|
Matches any non-word character
|
\d
|
Matches any digit
|
\D
|
Matches any non-digit
|
\b
|
Matches a word boundary
|
\B
|
Matches a non-word boundary
|
\cX
|
Matches a control character. Example g:\cd matches control-d
|
\xhh
|
Matches the character with two characters with hexadecimal code hh
|
\uhhh
|
Matches the Unicode character with four characters of hexadecimal
code hhhh
|
Character Sets
Pattern
|
Description
|
[character],[^character]
|
[character] matches all the contained character
|
|
[^character] matches all but the contained characters
|
List of Quantifiers of the RegExp Object
Quantifier
|
Description
|
n+
|
Represents a string which contains at least one n
|
n*
|
Represents a string that contains zero or more occurrences of n
|
n?
|
Represents a string that contains zero or one occurrences of n
|
n{a}
|
Represents a string that contains n, a number of times
|
n{a,b}
|
Represents a string that contains n, a to b number of times
|
n{a,}
|
Represents a string that contains n, a or more number of times
|
n$
|
Represents a string that ends with n
|
^n
|
Represents a string that starts with a
|
?=n
|
Represents a string that is followed by specific string n
|
?!n
|
Represents a string that is not followed by specific string n
|
Properties of RegExp Object
Property
|
Description
|
global
|
Refers that the g modifier is set
|
ignoreCase
|
Refers that the I modifier is set
|
lastIndex
|
Refers the index to start the next match
|
multiline
|
Refers that the m modifier is set
|
source
|
Refers the text of RegExp pattern
|
Methods of RegExp object
Method
|
Description
|
compile
|
Compiles the RegExp object
|
exec
|
Tests for the match in a string and returns a result array
|
test
|
Tests for a match in a string and returns true or false
|
0 comments:
Post a Comment