Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

JavaScript Regular Expression

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

JavaScript Regular Expression

Uploaded by

Dewang Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript Regular

Expression
Regular Expressions and RegExp Object
• A regular expression is an object that describes a pattern of characters.
• The JavaScript RegExp class represents regular expressions, and both
String and RegExp define methods that use regular expressions to perform
powerful pattern-matching and search-and-replace functions on text.
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/i
• /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).
Modifiers
• Modifiers are used to perform case-insensitive and global searches:
Modifier Description
g Perform a global match (find all matches rather than stopping after the first match)
i Perform case-insensitive matching
m Perform multiline matching

Brackets
• Brackets are used to find a range of characters:

Expression Description
[....] Find any character between the brackets
[^....] Find any character NOT between the brackets
[0-9] Find any digit between 0 to 9
[a-z] Find any character between lower case a to lowercase z
[A-Z] Find any character between Upper case A to Uppercase Z
Quantifiers
• The frequency or position of bracketed character sequences and single characters can be denoted
by a special character.
• Each special character has a specific connotation. The +, *, ?, and $ flags all follow a character
sequence.
Quantifier Description
p+ It matches any string containing one or more p's.
p* It matches any string containing zero or more p's.
p? It matches any string containing at most one p's.
p{N} It matches any string containing a sequence of N p's
p{2,3} It matches any string containing a sequence of two or three p's
p{2,} It matches any string containing a sequence of at least two p's.
p$ It matches any string with p at the end of it.
^p It matches any string with p at the beginning of it.
Example
[^a-zA-Z]---->It matches any string not containing any of the characters ranging from a through z
and A through Z.
^.{2}$---->It matches any string containing exactly two characters.
<b>(.*)</b>---->It matches any string enclosed within <b> and </b>.
RegExp Object Properties
Property Description
constructor Returns the function that created the RegExp object's
prototype
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
Metacharacters
• A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the
combination a special meaning.
• For instance, you can search for a large sum of money using the '\d' metacharacter: /([\d]+)000/, Here
\d will search for any string of numerical character.
Metacharacter Description
. Find a single character, except newline or line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b
\B Find a match, but not at the beginning/end of a word
\0 Find a NULL character
\n Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal number dd
\udddd Find the Unicode character specified by a hexadecimal
number dddd
[aeiou] matches a single character in the given set
[^aeiou] matches a single character outside the given set
(foo|bar|baz) matches any of the alternatives specified
RegExp Object Methods
Method Description
compile() Deprecated in version 1.5. Compiles a regular
expression
exec() Tests for a match in a string. Returns the first
match
test() Tests for a match in a string. Returns true or false
toString() Returns the string value of the regular expression
Regular expression for password validation
RegEx Description
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.[!@#\$%\^&]) The string must contain at least one special character, but
we are escaping reserved RegEx characters to avoid conflict
(?=.{8,}) The string must be eight characters or longer
Example
/^ : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
$/ : End
var regularExpression = /^(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/;

You might also like