14
JavaScript Regular Expression

Javascript regular expression

Embed Size (px)

Citation preview

Page 1: Javascript regular expression

JavaScript Regular

Expression

Page 2: 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.

Page 3: Javascript regular expression

Syntax for Regular Expression

• Var pattern=/….Expression …/

Page 4: Javascript regular expression
Page 5: Javascript regular 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]/

Page 6: Javascript regular expression
Page 7: Javascript regular expression
Page 8: Javascript regular expression

Examples

• Pattern for age

var pattern=/^\d{1,2}$/

• Pattern for mobile number.

var pattern=/^\d{10}$/; 

Page 9: Javascript regular expression

Metacharacter

• A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning.

Page 10: Javascript regular expression
Page 11: Javascript regular expression
Page 12: Javascript regular expression

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 _

Page 13: Javascript regular expression

• 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

Page 14: Javascript regular expression

• Characters Description

+ indicates expression occur one or more times e.g.

.com, co.us

$ matches end of string