Professional Documents
Culture Documents
Krishna Bhati
JavaScript Regular Expression
• Regular Expressions are a powerful tool used in pattern-matching
and substitution.
• When you search in a text, you can use a pattern to describe what
you are searching for.
• They are commonly associated with almost all UNIX-based tools,
including editors like vi, scripting languages like Perl and PHP, and
Shell programs like awk and sed
• Javascript also supports Regular Expressions
• A Regular Expression lets you build patterns using a set of special
characters.
• Depending on whether or not there's a match, appropriate action
can be taken, and appropriate program code executed.
2
Regular expressions and Form Validation
3
RegExp Syntax
• var regExp= /pattern/[switch]
• In a Regular Expression /pattern/ is a Regular Expression and
[switch] (optional) indicates the mode in which the Regular
Expression is to be used:
• "i" - ignore case,
"g" - global search,
"gi" - global search + ignore case (case-insensitive).
• RegExp Quantifiers - "meta-characters"
• Usually used for more complex regular expressions.
• We can use "meta-characters", special characters, that have a
special meaning when used within a pattern.
4
RegExp()
• The constructor function is used as follows:
• new RegExp("pattern"[, "flags"])
Parameters:
• pattern
The text of the regular expression.
• flags
If specified, flags can have one of the following values:
g: global match
i: ignore case
gi: both global match and ignore case
m: Perform multiline matching
5
6
RegExp Quantifiers - "meta-characters"
RegExp Quantifiers - "meta-characters"
+ "+" is used to match one or more
/bo+/ occurrence of the preceding character.
would match the words "bore", "boom", and
"bookstore"., though not burry, or pound
* "*" is used to match zero or more
/mat*/ occurrences of the preceding character.
would match "ma", "mat" and "matter".
7
RegExp Special Characters
• It's also possible to search for whitespace,
numbers and alphabetic characters with a
Regular Expression.
\s used to match a single whitespace character,
including tabsthese
and newline characters
• The following lists special characters:
\S used to match everything that is not a
whitespace character
\d used to match numbers from 0 to 9
9
Alternate Way
• simpler way to add pattern anchors to your expression
– \b
• This is used to check that the RegExp matches the boundary of a
string, and it can be placed either at the beginning or end of the
pattern to be matched.
• /\bhom/
• would match both "home" and "homestead", while
• /man\b/
• would match "human", "woman" and "man", though not "manor"
or "manners".
• And the converse of this is \B, which matches everywhere but at the
boundaries of a string.
10
Examples(Pick up your pens)
1. "Charles the Brit raced his moped through the park.“
2. "The Park Ranger watched Charles do this"
Regular expression examples
Canvas
– The canvas element is used to draw graphics on a web page.
What is Canvas?
– The HTML5 canvas element uses JavaScript to draw graphics on a web page.
– A canvas is a rectangular area, and you control every pixel of it.
– The canvas element has several methods for drawing paths, boxes, circles,
characters, and adding images.
Create a Canvas Element
– Add a canvas element to the HTML5 page.
– Specify the id, width, and height of the element:
Syntax
<canvas id="myCanvas" width="200" height="100"></canvas>
JavaScript – Graphics
Canvas
<script type="text/javascript“>
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
ctx.fillStyle='green';
ctx.fillRect(0,0,150,100);
</script>
<canvas id="myCanvas">your browser does not support the canvas tag </canvas>
The getContext("2d") object is a built-in HTML5 object, with many methods to draw
paths, boxes, circles, characters, images and more.
JavaScript – Graphics
Canvas
Understanding Coordinates
– The fillRect method above had the parameters (0,0,150,100).
– This means: Draw a 150x100 rectangle on the canvas, starting at the top left
corner (0,0).
– The canvas' X and Y coordinates are used to position drawings on the canvas.
Example
Canvas - Paths
• To draw straight lines on a canvas, we will use the following two methods:
• moveTo(x,y) defines the starting point of the line
• lineTo(x,y) defines the ending point of the line
• To actually draw the line, we must use one of the "ink" methods, like
stroke().
• Example
• Define a starting point in position (0,0), and an ending point in position
(200,100). Then use the stroke() method to actually draw the line:
• JavaScript:
• var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
Your Logo
JavaScript – Graphics
Canvas
• To actually draw the circle, we must use one of the "ink" methods, like stroke() or fill().
• We can use the beginPath() method each time we want to start drawing a new path.
Make sure you keep the arc() and rect() calls in between calls to beginPath() and
closePath().
Canvas - Text
• var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("scream");
ctx.drawImage(img,10,10);
Your Logo
THANK YOU
20