Javascript Regular Expressions and Exception Handling: Prof - N. Nalini Ap (SR) Scope Vit Vellore
Javascript Regular Expressions and Exception Handling: Prof - N. Nalini Ap (SR) Scope Vit Vellore
and
Exception Handling
Prof . N. Nalini
AP(Sr)
SCOPE
VIT
VELLORE
What is a regular expression?
/[a-zA-Z_\-]+@(([a-zA-Z_\-])+\.)+[a-zA-Z]{2,4}/
3
Regex flags / modifiers
/pattern/g global; match/replace all occurrences
/pattern/i case-insensitive
/pattern/m multi-line mode
/pattern/y "sticky" search, starts from a given index
4
Basic regexes
/abc/
5
String match
string.match(regex)
7
Special characters
| means OR
/abc|def|g/ matches lines with "abc", "def", or "g"
precedence: ^Subject|Date: vs. ^(Subject|Date):
There's no AND & symbol.
9
10
Quantifiers: * + ?
* means 0 or more occurrences
/abc*/ matches "ab", "abc", "abcc", "abccc", ...
/a(bc)/" matches "a", "abc", "abcbc", "abcbcbc", ...
/a.*a/ matches "aa", "aba", "a8qa", "a!?_a", ...
? means 0 or 1 occurrences
/Martina?/ matches lines with "Martin" or "Martina"
/Dan(iel)?/ matches lines with "Dan" or "Daniel"
11
More quantifiers
{min,max} means between min and max occurrences
/a(bc){2,4}/ matches lines that contain
"abcbc", "abcbcbc", or "abcbcbcbc"
12
Character sets
[ ] group characters into a character set;
will match any single character from the set
/[bcd]art/ matches lines with "bart", "cart", and "dart"
equivalent to /(b|c|d)art/ but shorter
13
Character ranges
• inside a character set, specify a range of chars with -
/[a-z]/ matches any lowercase letter
/[a-zA-Z0-9]/ matches any letter or digit
variations
– (?:text) match text but don't capture
– a(?=b) capture pattern b but only if preceded by a
– a(?!b) capture pattern b but only if not preceded by a
18
Replacing with back-references
• you can use back-references when replacing text:
refer to captures as $number in the replacement string
Example: to swap a last name with a first name:
var name = "Durden, Tyler";
name = name.replace(/(\w+),\s+(\w+)/, "$2 $1");
// "Tyler Durden"
19
The RegExp object
new RegExp(string)
new RegExp(string, flags)
20
exec() and test()
• exec() - Tests for a match in a string. Returns the first
match
21
Working with RegExp
• in a regex literal, forward slashes must be \ escaped:
/http[s]?:\/\/\w+\.com/
23
JavaScript Exception
• When an error occurs, JavaScript will normally stop and
generate an error message.
• The try statement allow to define a block of code to be
tested for errors while it is being executed.
• The catch statement allows to define a block of code to
be executed, if an error occurs in the try block.
• The JavaScript statements try and catch come in pairs:
24
JavaScript Error Types
• The Six JavaScript Error Types
EvalError: Raised when the eval() functions is used in an incorrect manner.
RangeError: Raised when a numeric variable exceeds its allowed range.
ReferenceError: Raised when an invalid reference is used.
SyntaxError: Raised when a syntax error occurs while parsing JavaScript code.
TypeError: Raised when the type of a variable is not as expected.
URIError: Raised when the encodeURI() or decodeURI() functions are used in an
incorrect manner.
EX:2
26
JavaScript Exception
The throw Statement
• The throw statement allows you to create a custom error.
• Technically you can throw an exception (throw an error).
• The exception can be a JavaScript String, a Number, a Boolean or an Object:
27
28
The finally Statement
29
30