A regular expression is a pattern used to match character combinations in text and replace or search text. Regular expressions use special characters like /, ^, $, ., +, ?, *, etc. to match patterns like letters, numbers, email addresses, mobile numbers. For example, the pattern /^\d{10}$/ matches a 10 digit mobile number and /^\w+([\.-]?\w+)*@\w+([\.]?\w+)*(\.\w{2,3})+$/ validates an email address format.
2. • A regular expression is an object that describes a
pattern of characters.
• regular expressions is used to perform patternmatching and search-and-replace on text.
5. 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]/
6.
7.
8. Examples
• Pattern for age
var pattern=/^d{1,2}$/
• Pattern for mobile number.
var pattern=/^d{10}$/;
9. Metacharacter
• A metacharacter is simply an alphabetical character
preceded by a backslash that acts to give the
combination a special meaning.
10.
11.
12. Validation email ID
• Var pattern=/^w+([.-]?w+)*@w+([.]?w+)*(.w{2,3})+$/
Characters
Description
/../
all regular expression start
with forward slash
^
w+
.-
matches beginning of string
matches one or more word char
indicate next char is special
matches . Or _
and end
13. • Characters
?
or 1 time
Description
Matches previous char 0
([.-]?w+)*
matches 0 or more
occurences of [.-]w+
@
matches @only
(.w{2,3})
it matches . Followed by 2
or 3 word char