🗊Презентация Module 8. Regular Expressions

Нажмите для полного просмотра!
Module 8. Regular Expressions, слайд №1Module 8. Regular Expressions, слайд №2Module 8. Regular Expressions, слайд №3Module 8. Regular Expressions, слайд №4Module 8. Regular Expressions, слайд №5Module 8. Regular Expressions, слайд №6Module 8. Regular Expressions, слайд №7Module 8. Regular Expressions, слайд №8Module 8. Regular Expressions, слайд №9Module 8. Regular Expressions, слайд №10Module 8. Regular Expressions, слайд №11Module 8. Regular Expressions, слайд №12Module 8. Regular Expressions, слайд №13Module 8. Regular Expressions, слайд №14Module 8. Regular Expressions, слайд №15Module 8. Regular Expressions, слайд №16Module 8. Regular Expressions, слайд №17Module 8. Regular Expressions, слайд №18Module 8. Regular Expressions, слайд №19Module 8. Regular Expressions, слайд №20Module 8. Regular Expressions, слайд №21Module 8. Regular Expressions, слайд №22Module 8. Regular Expressions, слайд №23Module 8. Regular Expressions, слайд №24Module 8. Regular Expressions, слайд №25Module 8. Regular Expressions, слайд №26Module 8. Regular Expressions, слайд №27Module 8. Regular Expressions, слайд №28

Вы можете ознакомиться и скачать презентацию на тему Module 8. Regular Expressions. Доклад-сообщение содержит 28 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Module 8:
Regular Expressions
Описание слайда:
Module 8: Regular Expressions

Слайд 2





Agenda
What is "Regular Expression"?
Creating and running regular expressions in JavaScript
Constructing regular expressions:
Part I: Exact and character set match, basic special characters
Part II: Quantifiers, controlling greedy and non-greedy capturing, capturing groups and logical operators
Useful links
Описание слайда:
Agenda What is "Regular Expression"? Creating and running regular expressions in JavaScript Constructing regular expressions: Part I: Exact and character set match, basic special characters Part II: Quantifiers, controlling greedy and non-greedy capturing, capturing groups and logical operators Useful links

Слайд 3







What is 
"Regular Expression"?
Описание слайда:
What is "Regular Expression"?

Слайд 4





Concept of Regular Expressions
Regular Expression (Regexp or Regex) is a special sequence of characters that forms a search pattern.
The concept of Regex has been created in 1950s by American mathematician Stephen Kleene who formalized the description of a regular language.
Now Regexes are widely used to verify or extract required data and much more
Описание слайда:
Concept of Regular Expressions Regular Expression (Regexp or Regex) is a special sequence of characters that forms a search pattern. The concept of Regex has been created in 1950s by American mathematician Stephen Kleene who formalized the description of a regular language. Now Regexes are widely used to verify or extract required data and much more

Слайд 5





Understanding basics
Rexes string looks like "cent(er|re)" 
Each character in Regex may be one of two types:
Regular character with its literal meaning
Special metacharacter with special meaning
In Regex "cent(er|re)" characters colored with green are regular characters with literal meaning while colored with red are metacharacters with special meaning
The Regex in example matches word "center" or "centre" in American or British spelling
Описание слайда:
Understanding basics Rexes string looks like "cent(er|re)" Each character in Regex may be one of two types: Regular character with its literal meaning Special metacharacter with special meaning In Regex "cent(er|re)" characters colored with green are regular characters with literal meaning while colored with red are metacharacters with special meaning The Regex in example matches word "center" or "centre" in American or British spelling

Слайд 6







Creating and running regular expressions in JavaScript
Описание слайда:
Creating and running regular expressions in JavaScript

Слайд 7





Create RegExp object
JavaScript has special object RexExp 
There are two ways to create an instance of it:
var myRE = new RegExp('SomeExpression');
var myRE = /SomeExpression/;
Important note: Variant 2 is preferred because special characters for string formatting are ignored here
Описание слайда:
Create RegExp object JavaScript has special object RexExp There are two ways to create an instance of it: var myRE = new RegExp('SomeExpression'); var myRE = /SomeExpression/; Important note: Variant 2 is preferred because special characters for string formatting are ignored here

Слайд 8





RegExp flags
Regular expressions have four optional flags that allow for global and case insensitive searching. 
To indicate a global search, use the g flag. 
To indicate a case-insensitive search, use the i flag. 
To indicate a multi-line search, use the m flag. 
To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. 
These flags can be used separately or together in any order, and are included as part of the regular expression.
To include a flag with the regular expression, use this syntax:
var re = /pattern/flags;
or
var re = new RegExp("pattern", "flags");

Note that the flags are an integral part of a regular expression. They cannot be added or removed later.
Описание слайда:
RegExp flags Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression. To include a flag with the regular expression, use this syntax: var re = /pattern/flags; or var re = new RegExp("pattern", "flags"); Note that the flags are an integral part of a regular expression. They cannot be added or removed later.

Слайд 9





RegExp method test()
To check if RegExp matches the string we should use test() method of RegExp object. 
This method accepts string and returns true if it finds a match, otherwise it returns false.
Example
Search a string for the character "o":
var str = "Hello";
var re = /o/;
var result = re.test(str);
Variable "result" is true
Описание слайда:
RegExp method test() To check if RegExp matches the string we should use test() method of RegExp object. This method accepts string and returns true if it finds a match, otherwise it returns false. Example Search a string for the character "o": var str = "Hello"; var re = /o/; var result = re.test(str); Variable "result" is true

Слайд 10





String method search()
To find index in a string which corresponds match for regular expression we should use method search() of a String object.
If successful, search returns the index of the first match of the regular expression inside the string. Otherwise, it returns -1.
Example
Find a position of the character "o" in a string:
var str = "Hello";
var re = /o/;
var result = str.search(re);
Variable "result" is 4
Описание слайда:
String method search() To find index in a string which corresponds match for regular expression we should use method search() of a String object. If successful, search returns the index of the first match of the regular expression inside the string. Otherwise, it returns -1. Example Find a position of the character "o" in a string: var str = "Hello"; var re = /o/; var result = str.search(re); Variable "result" is 4

Слайд 11





String method match()
To extract all matches of regular expression from a string, use method match() of a String object. It accepts RegExp as a parameter and returns an array containing all matches or null if there where no matches.
Syntax:
str.match(regexp);
Important note: regular expression should include "g" flag, otherwise method will work same way as RegExp.exec() method which require a loop to get all matches.
Example
Extract all characters "o" from a string:
var str = "Hello World!";
var re = /o/g;
var result = str.match(re);
Variable "result" is ["o", "o"]
Описание слайда:
String method match() To extract all matches of regular expression from a string, use method match() of a String object. It accepts RegExp as a parameter and returns an array containing all matches or null if there where no matches. Syntax: str.match(regexp); Important note: regular expression should include "g" flag, otherwise method will work same way as RegExp.exec() method which require a loop to get all matches. Example Extract all characters "o" from a string: var str = "Hello World!"; var re = /o/g; var result = str.match(re); Variable "result" is ["o", "o"]

Слайд 12





Using Regexes with HTML Forms
HTML5 supports special attribute pattern for <input> elements of a form. 
The pattern attribute specifies a regular expression that the <input> element's value is checked against.
The pattern attribute works with the following input types: text, search, url, tel, email, and password.
Use the title attribute to describe the pattern to help the user.
Example form:
<form>
  Car number: <input type="text" name="car" 
   pattern="[A-Z]{2}\d{4}[A-Z]{2}"
   title="Format: AB1234CD">
   <input type="submit">
</form>
Описание слайда:
Using Regexes with HTML Forms HTML5 supports special attribute pattern for <input> elements of a form. The pattern attribute specifies a regular expression that the <input> element's value is checked against. The pattern attribute works with the following input types: text, search, url, tel, email, and password. Use the title attribute to describe the pattern to help the user. Example form: <form> Car number: <input type="text" name="car" pattern="[A-Z]{2}\d{4}[A-Z]{2}" title="Format: AB1234CD"> <input type="submit"> </form>

Слайд 13







Constructing Regular 
Expressions 
Exact and character set match, 
basic special characters
Описание слайда:
Constructing Regular Expressions Exact and character set match, basic special characters

Слайд 14





Exact match and anchors
Checking for EXACT match of some pattern string inside ANY place of the test string:
var re = /pattern/;
re.test("some string to test pattern inside"); // true
re.test("there is no test string inside'); // false
Checking for EXACT match of pattern string from the BEGINNING of the test string:
var re = /^pattern/;
re.test("pattern starts string"); // true
re.test("there is no pattern at the beginning"); // false
Checking for EXACT match of pattern string at the END of the test string:
var re = /pattern$/;
re.test("string ends with pattern"); // true
re.test("there is no pattern at the end"); // false
Описание слайда:
Exact match and anchors Checking for EXACT match of some pattern string inside ANY place of the test string: var re = /pattern/; re.test("some string to test pattern inside"); // true re.test("there is no test string inside'); // false Checking for EXACT match of pattern string from the BEGINNING of the test string: var re = /^pattern/; re.test("pattern starts string"); // true re.test("there is no pattern at the beginning"); // false Checking for EXACT match of pattern string at the END of the test string: var re = /pattern$/; re.test("string ends with pattern"); // true re.test("there is no pattern at the end"); // false

Слайд 15





Character set match
Checking for ONE OR MORE symbols at ANY place in ANY order of the test string:
var re = /[abc]/;
re.test("bac"); // true
re.test("baac"); // true
re.test("bdac"); // true
re.test("fpirufieuhfa"); // true
re.test("sdfgsdfg"); // false
SAME but only at the BEGINNING:
var re = /^[abc]/;
re.test("bac"); // true
re.test("baac"); // true
re.test("fpirufieuhfa"); // false
re.test("sdfgsdfg"); false
SAME idea at the END
Описание слайда:
Character set match Checking for ONE OR MORE symbols at ANY place in ANY order of the test string: var re = /[abc]/; re.test("bac"); // true re.test("baac"); // true re.test("bdac"); // true re.test("fpirufieuhfa"); // true re.test("sdfgsdfg"); // false SAME but only at the BEGINNING: var re = /^[abc]/; re.test("bac"); // true re.test("baac"); // true re.test("fpirufieuhfa"); // false re.test("sdfgsdfg"); false SAME idea at the END

Слайд 16





Negated character set
Using caret symbol "^" as first character set symbol "negates" it, change meaning to opposite:
var re = /[^abc]/;
re.test("bac"); // false
re.test("baac"); // false
re.test("bdac"); // true
re.test("fpirufieuhfa"); // true
re.test("sdfgsdfg"); // true
Описание слайда:
Negated character set Using caret symbol "^" as first character set symbol "negates" it, change meaning to opposite: var re = /[^abc]/; re.test("bac"); // false re.test("baac"); // false re.test("bdac"); // true re.test("fpirufieuhfa"); // true re.test("sdfgsdfg"); // true

Слайд 17





Special character: \
A backslash that precedes a non-special character indicates that the next character is special and is not to be interpreted literally. 
For example, a 'b' without a preceding '\' generally matches lowercase 'b's wherever they occur. But a '\b' by itself doesn't match any character; it forms the special word boundary character.
A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally. For example, the pattern /a*/ relies on the special character '*' to match 0 or more a's. By contrast, the pattern /a\*/ removes the specialness of the '*' to enable matches with strings like 'a*'.
Do not forget to escape \ itself while using the RegExp("pattern") notation because \ is also an escape character in strings.
Описание слайда:
Special character: \ A backslash that precedes a non-special character indicates that the next character is special and is not to be interpreted literally. For example, a 'b' without a preceding '\' generally matches lowercase 'b's wherever they occur. But a '\b' by itself doesn't match any character; it forms the special word boundary character. A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally. For example, the pattern /a*/ relies on the special character '*' to match 0 or more a's. By contrast, the pattern /a\*/ removes the specialness of the '*' to enable matches with strings like 'a*'. Do not forget to escape \ itself while using the RegExp("pattern") notation because \ is also an escape character in strings.

Слайд 18





Symbol ranges
How to specify symbol ranges?
var re = /[a-z]/; // from a to z
var re = /[a-zA-Z]// from a to Z
var re = /[0-9]/; // from 0 to 9
Using special characters for symbol ranges:
\w - Matches any alphanumeric character including the underscore.
\W - Matches any non-word character. Equivalent to [^A-Za-z0-9_]. 
/\w/ == /[a-zA-Z0-9_]/ 
/\W/ == /[^A-Za-z0-9_]/
/\d/ == /[0-9]/
Описание слайда:
Symbol ranges How to specify symbol ranges? var re = /[a-z]/; // from a to z var re = /[a-zA-Z]// from a to Z var re = /[0-9]/; // from 0 to 9 Using special characters for symbol ranges: \w - Matches any alphanumeric character including the underscore. \W - Matches any non-word character. Equivalent to [^A-Za-z0-9_]. /\w/ == /[a-zA-Z0-9_]/ /\W/ == /[^A-Za-z0-9_]/ /\d/ == /[0-9]/

Слайд 19





Pattern .
(The decimal point) matches any single character except the newline character.
For example, /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but not 'nay'.
Описание слайда:
Pattern . (The decimal point) matches any single character except the newline character. For example, /.n/ matches 'an' and 'on' in "nay, an apple is on the tree", but not 'nay'.

Слайд 20







Constructing Regular 
Expressions 
Part II: Quantifiers, controlling greedy and 
non-greedy capturing, 
capturing groups and logical operators
Описание слайда:
Constructing Regular Expressions Part II: Quantifiers, controlling greedy and non-greedy capturing, capturing groups and logical operators

Слайд 21





Quantifiers and greedy capturing
Quantifiers show how many times preceding symbol should appear in the string:
Use curved brackets with one number inside to show how many times exactly symbol should appear: "{4}"  - means "4 times exactly"
Use curved brackets with pair of numbers like "{0, 3}" to show minimum and maximum number of times (from zero to three)
Use special quantifier symbols like "*", "+", "?" (explained later)
By default, RegExp engine behaves in greedy way and tries to capture as many symbols as possible to match the expression. If we want to capture fewest symbols possible we, we should add question mark after quantifier symbol (explained later).
Описание слайда:
Quantifiers and greedy capturing Quantifiers show how many times preceding symbol should appear in the string: Use curved brackets with one number inside to show how many times exactly symbol should appear: "{4}" - means "4 times exactly" Use curved brackets with pair of numbers like "{0, 3}" to show minimum and maximum number of times (from zero to three) Use special quantifier symbols like "*", "+", "?" (explained later) By default, RegExp engine behaves in greedy way and tries to capture as many symbols as possible to match the expression. If we want to capture fewest symbols possible we, we should add question mark after quantifier symbol (explained later).

Слайд 22





Quantifier *
Matches the preceding character 0 or more times. Equivalent to {0,}.
For example, /bo*/ matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted".
var re = /bo*/;
re.test("A ghost booooed"); // true
re.test("A bird warbled"); // true
re.test("A goat grunted"); // false
Описание слайда:
Quantifier * Matches the preceding character 0 or more times. Equivalent to {0,}. For example, /bo*/ matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted". var re = /bo*/; re.test("A ghost booooed"); // true re.test("A bird warbled"); // true re.test("A goat grunted"); // false

Слайд 23





Quantifier+
Matches the preceding character 1 or more times. Equivalent to {1,}.
var re = /bo+/; // OR var re = /bo{1,}/
re.test("A ghost booooed"); // true
re.test("A bird warbled"); // false
re.test("A goat grunted"); // false
re.test("A boat sank"); // true
Описание слайда:
Quantifier+ Matches the preceding character 1 or more times. Equivalent to {1,}. var re = /bo+/; // OR var re = /bo{1,}/ re.test("A ghost booooed"); // true re.test("A bird warbled"); // false re.test("A goat grunted"); // false re.test("A boat sank"); // true

Слайд 24





Quantifier ? and switching to non-greedy
Matches the preceding character 0 or 1 time. 
Equivalent to {0,1}.
var re = /e?le?/;
re.test("angel"); // true (el)
re.test("angle"); // true (le)
re.test("oslo"); // true
If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the fewest possible characters), as opposed to the default, which is greedy (matching as many characters as possible). For example, applying /\d+/ to "123abc" matches "123". But applying /\d+?/ to that same string matches only the "1".
Описание слайда:
Quantifier ? and switching to non-greedy Matches the preceding character 0 or 1 time. Equivalent to {0,1}. var re = /e?le?/; re.test("angel"); // true (el) re.test("angle"); // true (le) re.test("oslo"); // true If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the fewest possible characters), as opposed to the default, which is greedy (matching as many characters as possible). For example, applying /\d+/ to "123abc" matches "123". But applying /\d+?/ to that same string matches only the "1".

Слайд 25





Capturing groups and logical operators
Round brackets "(",")" used to define capturing groups which mean a sub-expression inside regular expression
Often used with logical OR operator "|"
Example:
var str = "One man but many men";
var re = /m(a|e)n/g;
var result = str.match(re);
Variable "result" is ["man", "man", "men"]
Описание слайда:
Capturing groups and logical operators Round brackets "(",")" used to define capturing groups which mean a sub-expression inside regular expression Often used with logical OR operator "|" Example: var str = "One man but many men"; var re = /m(a|e)n/g; var result = str.match(re); Variable "result" is ["man", "man", "men"]

Слайд 26







Useful links
Описание слайда:
Useful links

Слайд 27






RegEx101: http://regex101.com
Regular Expressions on Mozilla Developer Network:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
RexEgg: http://www.rexegg.com/
Описание слайда:
RegEx101: http://regex101.com Regular Expressions on Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions RexEgg: http://www.rexegg.com/

Слайд 28







Thank you!
Описание слайда:
Thank you!



Похожие презентации
Mypresentation.ru
Загрузить презентацию