Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©2005  Department of Computer & Information Science JavaScript Regular Expressions.

Similar presentations


Presentation on theme: "Copyright ©2005  Department of Computer & Information Science JavaScript Regular Expressions."— Presentation transcript:

1 Copyright ©2005  Department of Computer & Information Science JavaScript Regular Expressions

2 Copyright ©2005  Department of Computer & Information Science Goals By the end of this unit you should … Understand what regular expressions areUnderstand what regular expressions are Be able to use regular expressions to match text against a particular string patternBe able to use regular expressions to match text against a particular string pattern Be able to use special regular expression characters to match multiple search terms against stringsBe able to use special regular expression characters to match multiple search terms against strings

3 Copyright ©2005  Department of Computer & Information Science What is a Regular Expression? A regular expression is a pattern of characters.A regular expression is a pattern of characters. We use regular expressions to search for matches on particular text.We use regular expressions to search for matches on particular text. In JavaScript, we can use regular expressions by creating instances of the regular expression object, RegExp.In JavaScript, we can use regular expressions by creating instances of the regular expression object, RegExp.

4 Copyright ©2005  Department of Computer & Information Science The Regular Expression Constructor We can declare a regular expression by using a constructor. General Form: var regExpName = new RegExp(“RegExp”, “flags”);We can declare a regular expression by using a constructor. General Form: var regExpName = new RegExp(“RegExp”, “flags”); Example: var searchTermRE = new RegExp(“s”,“gi”); (search for the letter “s” globally, ignore case)Example: var searchTermRE = new RegExp(“s”,“gi”); (search for the letter “s” globally, ignore case)

5 Copyright ©2005  Department of Computer & Information Science The Regular Expression Literal Declaring using a reg. expression literal: var searchTermRE = /X1X4/gi;Declaring using a reg. expression literal: var searchTermRE = /X1X4/gi; When declaring regular expression literals, do NOT include quotation marks and offset the expression with a pair of forward slashes. By convention, variables acting as regular expressions end with the suffix “RE.” Flags come after the second forward slash.When declaring regular expression literals, do NOT include quotation marks and offset the expression with a pair of forward slashes. By convention, variables acting as regular expressions end with the suffix “RE.” Flags come after the second forward slash.

6 Copyright ©2005  Department of Computer & Information Science Literal Characters A lot of the time, we use regular expressions to match specific patterns, like the word “java”: var firstRE = /java/; (would match the words “java”, “javascript”, “javabeans”, “myJava”)A lot of the time, we use regular expressions to match specific patterns, like the word “java”: var firstRE = /java/; (would match the words “java”, “javascript”, “javabeans”, “myJava”) Matching character for character is termed matching literals.Matching character for character is termed matching literals.

7 Copyright ©2005  Department of Computer & Information Science Non-Printing Literal Characters We also consider some non-printing characters as literals:We also consider some non-printing characters as literals: –\t (tab character) –\n (newline character) –\0 (NUL character – null value)

8 Copyright ©2005  Department of Computer & Information Science Metacharacters Sometimes, we want to search not for specific patterns, but for parts of patterns.Sometimes, we want to search not for specific patterns, but for parts of patterns. Consider searching for all lines that end with the letter “s”. To do so, we’ll need to use metacharacters: var firstRE = /s$/; (finds all phrases that end with “s”)Consider searching for all lines that end with the letter “s”. To do so, we’ll need to use metacharacters: var firstRE = /s$/; (finds all phrases that end with “s”)

9 Copyright ©2005  Department of Computer & Information Science What are Metacharacters? Metacharacters are characters used to represent special patterns that don’t necessarily fit in the range of standard letters and numbers (A-Z; a-z; 0-9, etc.).Metacharacters are characters used to represent special patterns that don’t necessarily fit in the range of standard letters and numbers (A-Z; a-z; 0-9, etc.). We often use symbols as metacharacters to indicate a special circumstance.We often use symbols as metacharacters to indicate a special circumstance. Some of these symbols include: $. ^ * ?Some of these symbols include: $. ^ * ?

10 Copyright ©2005  Department of Computer & Information Science Metacharacters as Literals What if I want to search for a literal symbol that is also used as a metacharacters? To search for a symbol as a literal and not as a metacharacter, we use the \ (backslash) to turn “off” the metacharacter property. $ used as a metacharacter: var firstRE = /s$/; $ used as a literal character: var firstRE = /\$/;What if I want to search for a literal symbol that is also used as a metacharacters? To search for a symbol as a literal and not as a metacharacter, we use the \ (backslash) to turn “off” the metacharacter property. $ used as a metacharacter: var firstRE = /s$/; $ used as a literal character: var firstRE = /\$/;

11 Copyright ©2005  Department of Computer & Information Science Flags When searching, flags can help refine or expand a searchWhen searching, flags can help refine or expand a search Flags modify a particular search to fit certain criteriaFlags modify a particular search to fit certain criteria There are three common flags, the global flag, ignore case flag and the multiline mode flag.There are three common flags, the global flag, ignore case flag and the multiline mode flag.

12 Copyright ©2005  Department of Computer & Information Science The Global flag In a regular expression without flags, JavaScript will return only the first instance of a search term: var mySearchRE = /X1X4/; (returns only the first instance of “X1X4”)In a regular expression without flags, JavaScript will return only the first instance of a search term: var mySearchRE = /X1X4/; (returns only the first instance of “X1X4”) To modify the search to include all instances of “X1X4”, we would use the global flag: var mySearchRE = /X1X4/g; (returns all instances of “X1X4”)To modify the search to include all instances of “X1X4”, we would use the global flag: var mySearchRE = /X1X4/g; (returns all instances of “X1X4”)

13 Copyright ©2005  Department of Computer & Information Science The Ignore Case flag In a regular expression without flags, JavaScript only returns an exact match: var mySearchRE = /X1X4/; (returns only an instance of “X1X4”, but not “x1x4” or “x1X4”, etc.)In a regular expression without flags, JavaScript only returns an exact match: var mySearchRE = /X1X4/; (returns only an instance of “X1X4”, but not “x1x4” or “x1X4”, etc.) To modify the search to include instances of “X1X4”, regardless of case, we would use the ignore case flag: var mySearchRE = /X1X4/i; (returns an instance of “X1X4”,“x1x4”, x1X4”, etc.)To modify the search to include instances of “X1X4”, regardless of case, we would use the ignore case flag: var mySearchRE = /X1X4/i; (returns an instance of “X1X4”,“x1x4”, x1X4”, etc.)

14 Copyright ©2005  Department of Computer & Information Science The Multiline flag A single string may include newline characters. We can use the multiline flag allows us to search at the beginning or end of a line, not just the beginning or end of a string. To turn it on: var mySearchRE = /^X1X4/m;A single string may include newline characters. We can use the multiline flag allows us to search at the beginning or end of a line, not just the beginning or end of a string. To turn it on: var mySearchRE = /^X1X4/m;

15 Copyright ©2005  Department of Computer & Information Science Combining Flags We can also combine flags to expand our search: var mySearchRE = /X1X4/gi; (returns all instances of “x1x4”, “x1X4”, “X1x4” & “X1X4”)We can also combine flags to expand our search: var mySearchRE = /X1X4/gi; (returns all instances of “x1x4”, “x1X4”, “X1x4” & “X1X4”)

16 Copyright ©2005  Department of Computer & Information Science Searching for Matches Only at the Beginning of a Line Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this.Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this. The code: var mySearchRE = /^Jimmy/gm; ( would only return “Jimmy” from the first line)The code: var mySearchRE = /^Jimmy/gm; ( would only return “Jimmy” from the first line) The ^ metacharacter says “look only for matches at the beginning of the string or line (multiline mode).”The ^ metacharacter says “look only for matches at the beginning of the string or line (multiline mode).”

17 Copyright ©2005  Department of Computer & Information Science Searching for Matches Only at the End of a Line Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this.Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this. The code: var mySearchRE = /his$/gm; ( would only return “his” from the second line)The code: var mySearchRE = /his$/gm; ( would only return “his” from the second line) The $ metacharacter says “look only for matches at the end of the string or line (multiline mode).”The $ metacharacter says “look only for matches at the end of the string or line (multiline mode).”

18 Copyright ©2005  Department of Computer & Information Science Using Boundaries Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this.Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this. To search for the all instances of the word “the” we could use the space metacharacter ( \s ): var mySearchRE = /\sthe\s/gim; (Ignores “The” that begins the second line, since it has no space before it -- it starts a line)To search for the all instances of the word “the” we could use the space metacharacter ( \s ): var mySearchRE = /\sthe\s/gim; (Ignores “The” that begins the second line, since it has no space before it -- it starts a line)

19 Copyright ©2005  Department of Computer & Information Science Using Boundaries Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this.Consider the following string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this. Instead of using a space character, we can use the boundary ( \b ). The boundary metacharacter searches for all instances of a pattern which are not a prefix ( \b at the beginning of a search pattern) or a suffix ( \b at the end of a search pattern) of another word: var mySearchRE = /\bthe\b/gim;Instead of using a space character, we can use the boundary ( \b ). The boundary metacharacter searches for all instances of a pattern which are not a prefix ( \b at the beginning of a search pattern) or a suffix ( \b at the end of a search pattern) of another word: var mySearchRE = /\bthe\b/gim;

20 Copyright ©2005  Department of Computer & Information Science Using Boundaries (continued) Our string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this.Our string: Jimmy the Scot scooted his scooter through the Park. The park guard watched Jimmy do this. Code: var mySearchRE = /\bt/gim;Code: var mySearchRE = /\bt/gim; Search for all matches that begin with “t”. Ignore “t” if “t” is in the middle or at the end of a word.Search for all matches that begin with “t”. Ignore “t” if “t” is in the middle or at the end of a word.

21 Copyright ©2005  Department of Computer & Information Science Searching for Multiple Patterns at the Same Time Consider the following string: lop, mop, bop, sop, pop, gop, top, fopConsider the following string: lop, mop, bop, sop, pop, gop, top, fop To search for the all instances that end with “op” we would use a wildcard character (. ) There no need for the global flag, because the global is inherent in the wildcard character: var mySearchRE = /.op/; (returns the all the words)To search for the all instances that end with “op” we would use a wildcard character (. ) There no need for the global flag, because the global is inherent in the wildcard character: var mySearchRE = /.op/; (returns the all the words)

22 Copyright ©2005  Department of Computer & Information Science Searching for Multiple Patterns at the Same Time Consider the following string: lop, mop, bop, sop, pop, gop, top, fopConsider the following string: lop, mop, bop, sop, pop, gop, top, fop To search only for the instances that match “bop”, “lop” or “pop” we would use brackets to include the search characters, but exclude all others ( [] ): var mySearchRE = /[blp]op/;To search only for the instances that match “bop”, “lop” or “pop” we would use brackets to include the search characters, but exclude all others ( [] ): var mySearchRE = /[blp]op/;

23 Copyright ©2005  Department of Computer & Information Science Searching for Multiple Patterns at the Same Time Consider the following string: lop, mop, bop, sop, pop, gop, top, fopConsider the following string: lop, mop, bop, sop, pop, gop, top, fop We can also use ranges of letters in the brackets: var mySearchRE = /[a-m]op/; (returns “bop”, “fop”, “gop”, “lop” and “mop”, but ignores all other words ending with “op”)We can also use ranges of letters in the brackets: var mySearchRE = /[a-m]op/; (returns “bop”, “fop”, “gop”, “lop” and “mop”, but ignores all other words ending with “op”)

24 Copyright ©2005  Department of Computer & Information Science Excluding Patterns Consider the following string: lop, mop, bop, sop, pop, gop, top, fopConsider the following string: lop, mop, bop, sop, pop, gop, top, fop To search for the all instances that end with “op” except those that begin with “b”, “l” or “p”, we use the not metacharacter ( ^ ): var mySearchRE = /[^blp]op/; (returns the all the words except “bop”, “lop” and “pop”)To search for the all instances that end with “op” except those that begin with “b”, “l” or “p”, we use the not metacharacter ( ^ ): var mySearchRE = /[^blp]op/; (returns the all the words except “bop”, “lop” and “pop”) Inside brackets, the ^ symbol means “not” and DOES NOT mean the beginning of a line!Inside brackets, the ^ symbol means “not” and DOES NOT mean the beginning of a line!

25 Copyright ©2005  Department of Computer & Information Science Excluding Patterns Our string: lop, mop, bop, sop, pop, gop, top, fopOur string: lop, mop, bop, sop, pop, gop, top, fop We can also use ranges of letters in the brackets: var mySearchRE = /[^a-m]op/; (returns all words except “bop”, “fop”, “gop”, “lop” and “mop”)We can also use ranges of letters in the brackets: var mySearchRE = /[^a-m]op/; (returns all words except “bop”, “fop”, “gop”, “lop” and “mop”)

26 Copyright ©2005  Department of Computer & Information Science Other Metacharacters: ?, * and + To match zero or one characters: var mySearchRE = /b?onk/; (matches “bonk” or “onk”)To match zero or one characters: var mySearchRE = /b?onk/; (matches “bonk” or “onk”) To match zero or n characters: var mySearchRE = /b*onk/; (matches “bonk”, “onk” or “bbonk”)To match zero or n characters: var mySearchRE = /b*onk/; (matches “bonk”, “onk” or “bbonk”) To match one or n characters: var mySearchRE = /b+onk/; (matches “bonk” or “bbonk”, but not “onk”)To match one or n characters: var mySearchRE = /b+onk/; (matches “bonk” or “bbonk”, but not “onk”)

27 Copyright ©2005  Department of Computer & Information Science Other Metacharacters: { } To match a specific number of characters: var mySearchRE = /g{2}op/; (matches “goop”, but not “gop” or “gooop”)To match a specific number of characters: var mySearchRE = /g{2}op/; (matches “goop”, but not “gop” or “gooop”) To match between n and m characters: var mySearchRE = /g{1,3}p/; (matches “gop” “goop” or “gooop” only)To match between n and m characters: var mySearchRE = /g{1,3}p/; (matches “gop” “goop” or “gooop” only)

28 Copyright ©2005  Department of Computer & Information Science String.search() Method The String.search() method gives us the character position (index number) of where the search term starts or –1 if there is not match.The String.search() method gives us the character position (index number) of where the search term starts or –1 if there is not match. The String.search() does not perform global searches and will ignore the “ g ” flag!The String.search() does not perform global searches and will ignore the “ g ” flag!

29 Copyright ©2005  Department of Computer & Information Science Open the file called introRegExp_01.html

30 Copyright ©2005  Department of Computer & Information Science String.match() Method The String.match() method returns an array containing all of the matches from a string.The String.match() method returns an array containing all of the matches from a string. Unlike the String.search() method, the String.match() method does perform global searches.Unlike the String.search() method, the String.match() method does perform global searches.

31 Copyright ©2005  Department of Computer & Information Science Open the file called introRegExp_02.html

32 Copyright ©2005  Department of Computer & Information Science Summary We can use a regular expression to search for a pattern of characters.We can use a regular expression to search for a pattern of characters. We can create a JavaScript regular expression by using the RegExp constructor or by creating a regular expression literal.We can create a JavaScript regular expression by using the RegExp constructor or by creating a regular expression literal. continued …

33 Copyright ©2005  Department of Computer & Information Science Summary We can use the String.search() method to find the find first occurrence of a regular expression.We can use the String.search() method to find the find first occurrence of a regular expression. We can use the String.match() method to return an array of all occurrences of a regular expression.We can use the String.match() method to return an array of all occurrences of a regular expression.

34 Copyright ©2005  Department of Computer & Information Science Resources JavaScript: The Definitive Guide 4 th Edition by David Flanagan (O’Reilly, 2002)JavaScript: The Definitive Guide 4 th Edition by David Flanagan (O’Reilly, 2002)


Download ppt "Copyright ©2005  Department of Computer & Information Science JavaScript Regular Expressions."

Similar presentations


Ads by Google