410 likes | 618 Views
ECA 225. Applied Online Programming. regular expressions. Regular Expressions. another way to validate form input less code than String methods pattern matching based on Perl’s Regular Expressions find complex patterns within strings with just a few lines of code. Regular Expressions.
E N D
ECA 225 AppliedOnline Programming regular expressions ECA 225 Applied Interactive Programming
Regular Expressions • another way to validate form input • less code than String methods • pattern matching • based on Perl’s Regular Expressions • find complex patterns within strings with just a few lines of code ECA 225 Applied Interactive Programming
Regular Expressions • unique syntax • looks complex and mysterious • broken down into component parts, regular expressions are not so mysterious • In short, you create a pattern to look for /^\b\d{3}\-\d{2}\-\d{4}\b$/ ECA 225 Applied Interactive Programming
Regular Expressions • 2 ways to create a regular expression • object constructor, RegExp( ) • use new keyword • pass the pattern as an argument • assign result to variable var name = new RegExp( “Bob” ); ECA 225 Applied Interactive Programming
Regular Expressions • 2 ways to create a regular expression • assign pattern directly to a variable • delimit with forward slash rather than quotes, as you would a string • in this example, we have created a regular expression to match a string literal, “Bob” var name = /Bob/; ECA 225 Applied Interactive Programming
Regular Expressions • String methods, such as indexOf( ), are good for searching for exact matches • regular expressions allow us to match patterns, such as a social security number • we can do this with String methods such as indexOf( ) and charAt( ), but it is simpler with a regular expression xxx-xx-xxxx ECA 225 Applied Interactive Programming
Regular Expressions • regular expressions allow you to search for the correct pattern • define a pattern using • string literals • metacharacters • a character in an expression that is not matchable itself, but acts as a guideline for what is to be matched ECA 225 Applied Interactive Programming
Regular Expression example • Given the string • search the string to replace each instance of “all” with “the entire” • create a regular expression var myString = “Halle barked all night.”; var myRE = /all/; ECA 225 Applied Interactive Programming
Regular Expression example cont … • all regular expressions begin and end with a forward slash • use a RegExp or String method var myString = “Halle barked all night.”;var myRE = /all/; newString = myString.replace( myRE, “the entire”); document.write( myString + “<br>” + newString ); ECA 225 Applied Interactive Programming
String.replace( ) • takes 2 arguments • regular expression which takes the pattern to be matched • the string replacement when a match is found ECA 225 Applied Interactive Programming
Regular Expression example cont … • only one instance of the match was changed • as written the RE will match only the first instance of the pattern in the target string • to replace all instances of a match we add a switch to the RE • g mean global, all instances of the match • g goes after the last forward slash var myRE = /all/g; ECA 225 Applied Interactive Programming
Regular Expression example cont … • another problem: we do not want the word “all” to be replaced unless it is a complete word • metacharacter \b • word boundry • the pattern will match only if it is at a word boundary, ie, the beginning or end of a word var myRE = /\ball\b/g; ECA 225 Applied Interactive Programming
Regular Expression example cont … ECA 225 Applied Interactive Programming
Regular Expression test • all regular expressions begin and end with a forward slash • use a RegExp or String method var myString = “Halle barked all night.”;var myRE = /all/; newString = myString.replace( myRE, “REPLACE”); document.write( myString + “<br>” + newString ); ECA 225 Applied Interactive Programming
metacharacters gglobal, find all instances of the match isearch without case sensitivity var myRE = /all/ g; var myRE = /all/ i; ECA 225 Applied Interactive Programming
metacharacters cont … ^beginning of a string $ end of a string var myRE = /^hal/ i; var myRE = /ong.$/ i; ECA 225 Applied Interactive Programming
metacharacters cont … \bword boundary \B non-word boundary var myRE = /\ball/ i; var myRE = /\Ball/ i; ECA 225 Applied Interactive Programming
metacharacters cont … \xnnASCII character defined by nn [abcde] any one of the enclosed characters var myRE = /\x61/g; var myRE = /[H r]all/g; ECA 225 Applied Interactive Programming
metacharacters cont … [a-e]any character within the enclosed range [^abcde] any one of the characters not enclosed var myRE = /[a-j]/g; var myRE = /[^H r]all/g; ECA 225 Applied Interactive Programming
metacharacters cont … .any character except a newline \W any non-alphanumeric character var myRE = /.all./g; var myRE = /\W/g; ECA 225 Applied Interactive Programming
metacharacters cont … \wany alphanumeric character including underscore [A-Za-z0-9_ ] any alphanumeric character including underscore var myRE = /\w/g; var myRE = /[A-Za-z0-9_ ]/g; ECA 225 Applied Interactive Programming
metacharacters cont … \dany single digit \D any single non-digit var myRE = /\d/g; var myRE = /\D/g; ECA 225 Applied Interactive Programming
metacharacters cont … \sany single space \S any single non-space var myRE = /\s/g; var myRE = /\S/g; ECA 225 Applied Interactive Programming
metacharacters cont … {x}exactly x occurrences of the previous character {x,} x or more occurrences of the previous character var myRE = /l{2}/g; var myRE = /l{1,}/g; ECA 225 Applied Interactive Programming
metacharacters cont … {x,y}between x and y occurrences of the previous character ? 0 or 1 time var myRE = /l{1,2}/g; var myRE = /H?all/g; ECA 225 Applied Interactive Programming
metacharacters cont … *zero or more times + 1 or more times var myRE = /al*/g; var myRE = /l+[eo]/g; ECA 225 Applied Interactive Programming
metacharacters cont … |logical or ( ) grouping var myRE = /a(r|l)/g; var myRE = /a(r|l)\1/g; ECA 225 Applied Interactive Programming
escaped characters ECA 225 Applied Interactive Programming
re.test( ) • regular expression to test for SSN • test( ) will return T or F var mySSN = document.myForm.ssn.value;var myRE = /^\b\d{3}\-\d{2}\-\d{4}\b$/ ;result = myRE.test( mySSN );alert( result ); ECA 225 Applied Interactive Programming
JavaScript methods ECA 225 Applied Interactive Programming
String.replace( ) • takes 2 arguments • regular expression • replacement string to be used if a match is found var myString = “Halle barked all night.”;var myRE = /\ball\b/g; newString = myString.replace( myRE, “the entire”); document.write( myString + “<br>” + newString ); ECA 225 Applied Interactive Programming
String.search( ) • takes 1 argument • regular expression which contains the pattern to be matched • if a match is found • character offset is returned • if no match is found • – 1 is returned ECA 225 Applied Interactive Programming
String.search( ) cont … • example var myStr = "123-45-6789"; var re = /\-\d{2}\-/; var x = myStr.search( re ); document.write( myStr ); alert( x ); ECA 225 Applied Interactive Programming
String.match( ) • takes 1 argument • regular expression which contains the pattern to be matched • String.match( ) returns an array • elements of the array are all the values that match the pattern established by the Reg Exp • if no match is found, NULL is returned ECA 225 Applied Interactive Programming
String.match( ) cont … • example var myStr = "123-45-6789"; var re = /\-\d{2}\-/; var x = myStr.match( re ); document.write( myStr ); alert( x[ 0 ] ); alert( x[ 1 ] ); ECA 225 Applied Interactive Programming
RegExp.test( ) • takes 1 argument • the string to be tested • searches the string for a match to the Reg Exp • returns a Boolean value • TRUE or FALSE • used inside an if statement ECA 225 Applied Interactive Programming
RegExp.test( ) cont … • example var myStr = "123-45-6789"; var re = /^\b(\d{3})\-(\d{2})\-(\d{4})\b$/; var x = re.test( myStr ); if( x ) { alert( x );} ECA 225 Applied Interactive Programming
RegExp.exec( ) • takes 1 argument • the string to be tested • returns an array of matches found in the string • the value at index 0 is the original string • other indexes contain any values that were matched within parentheses ECA 225 Applied Interactive Programming
RegExp.exec( ) cont … • example var myStr = "123-45-6789"; var re = /^\b(\d{3})\-(\d{2})\-(\d{4})\b$/; var x = re.exec( myStr ); for( i = 0; i < x.length; i ++ ){ document.write(“<p>” + x[ i ] );} ECA 225 Applied Interactive Programming
backreference • parentheses have special meaning • grouping • setting precedence • backreferencing • stores the values matched inside parentheses for later use • use special characters to access stored values • $1 through $9 ECA 225 Applied Interactive Programming
backreference cont … • example var myStr = "123-45-6789"; var re = /^\b(\d{3})\-(\d{2})\-(\d{4})\b$/; document.write( myStr.replace( re,"The third match: $3 <br /> The second match: $2 <br /> The first match: $1" ) ); ECA 225 Applied Interactive Programming