Upload
dhairya-joshi
View
257
Download
0
Tags:
Embed Size (px)
Citation preview
JavaScript Regular
Expression
• A regular expression is an object that describes a pattern of characters.
• regular expressions is used to perform pattern-matching and search-and-replace on text.
Syntax for Regular Expression
• Var pattern=/….Expression …/
Examples
• Pattern for All lowercase letters
var pattern=/[a-z]/
• Pattern for All Uppercase letters
var pattern=/[A-Z]/
• Pattern for all letters
var pattern=/[a-zA-Z]/
• Pattern for only digits
var pattern=/[0-9]/
Examples
• Pattern for age
var pattern=/^\d{1,2}$/
• Pattern for mobile number.
var pattern=/^\d{10}$/;
Metacharacter
• A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.
Validation email ID• Var pattern=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\
w{2,3})+$/
Characters Description
/../ all regular expression start and end with forward slash
^ matches beginning of string
\w+ matches one or more word char
\.- \ indicate next char is special
matches . Or _
• Characters Description
? Matches previous char 0 or 1 time
([\.-]?\w+)* matches 0 or more occurences of [.-]\w+
@ matches @only
(\.\w{2,3}) it matches . Followed by 2 or 3 word char
• Characters Description
+ indicates expression occur one or more times e.g.
.com, co.us
$ matches end of string