🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

21-Regular expressions-14-08-2024.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Regular Expressions The RegExp Object A regular expression is a pattern of characters. The pattern is used for searching and replacing characters in strings. The RegExp Object is a regular expression ARUL VIT SCOPE with added P...

Regular Expressions The RegExp Object A regular expression is a pattern of characters. The pattern is used for searching and replacing characters in strings. The RegExp Object is a regular expression ARUL VIT SCOPE with added Properties and Methods. 1 Regular Expressions Modifiers Modifiers are used to perform case-insensitive and global searches: Modifier Description ARUL VIT SCOPE g Perform a global match (find all matches rather than stopping after the first match) i Perform case-insensitive matching m Perform multiline matching 2 Regular Expressions Brackets - Brackets are used to find a range of characters: Expression Description [abc] Find any character between the brackets [^abc] Find any character NOT between the brackets ARUL VIT SCOPE [0-9] Find any character between the brackets (any digit) [^0-9] Find any character NOT between the brackets (any non-digit) (x|y) Find any of the alternatives specified 5 Regular Expressions Metacharacters Metacharacters are characters with a special meaning: Metacharacter Description. Find a single character, except newline or line terminator \w Find a word character \W Find a non-word character ARUL VIT SCOPE \d Find a digit \D Find a non-digit character \s Find a whitespace character \S Find a non-whitespace character \b Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b 8 Regular Expressions Metacharacters Metacharacters are characters with a special meaning: \B Find a match, but not at the beginning/end of a word \0 Find a NULL character \n Find a new line character \f Find a form feed character ARUL VIT SCOPE \r Find a carriage return character \t Find a tab character \v Find a vertical tab character \xxx Find the character specified by an octal number xxx \xdd Find the character specified by a hexadecimal number dd \udddd Find the Unicode character specified by a hexadecimal number dddd 9. - Period A period matches any single character (except newline '\n'). Expression String Matched? a No match ac 1 match.. acd 1 match 2 matches acde (contains 4 characters) 10 Special Sequences Special sequences make commonly used patterns easier to write. Here's a list of special sequences: – \A - Matches if the specified characters are at the start of a string. Expression String Matched? the sun Match \Athe In the sun No match 12 Special Sequences \b - Matches if the specified characters are at the beginning or end of a word. Expression String Matched? football Match \bfoo a football Match a football No match the foo Match foo\b the afoo test Match the afootest No match 13 Special Sequences \B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a word. Expression String Matched? football No match \Bfoo a football No match a football Match the foo No match foo\B the afoo test No match the afootest Match 15 Special Sequences \d - Matches any decimal digit. Equivalent to [0-9] Expression String Matched? 12abc3 3 matches (at 12abc3) \d JavaScript No match 16 Special Sequences \D - Matches any non-decimal digit. Equivalent to [^0-9] Expression String Matched? 1ab34"50 3 matches (at 1ab34"50) \D 1345 No match 17 Special Sequences \s - Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v]. Expression String Matched? JavaScript RegEx 1 match \s JavaScriptRegEx No match 18 Special Sequences \S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\v]. Expression String Matched? ab 2 matches (at a b) \S No match 19 Special Sequences \w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_]. By the way, underscore _ is also considered an alphanumeric character. Expression String Matched? 12&": ;c 3 matches (at 12&": ;c) \w %"> ! No match 20 Special Sequences \W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_] Expression String Matched? Expression String Matched? 1a2%c 1 match (at 1a2%c) \W JavaScript No match 21 Special Sequences \Z - Matches if the specified characters are at the end of a string. Expression String Matched? I like JavaScript 1 match JavaScript\Z I like JavaScript No match Programming JavaScript is fun No match 23 Shortcodes for Other Metacharacters In addition to the metacharacters we have looked at, here are some of the most commonly used ones: \d – matches any decimal digit and is shorthand for [0-9]. \w – matches any alphanumeric character which could be a letter, a digit, or an underscore. – \w is shorthand for [A-Za-z0-9_]. \s – matches any white space character. \D – matches any non-digit and is the same as [^0-9.] \W – matches any non-word (that is non-alphanumeric) character and is shorthand for [^A-Za-z0-9_]. \S – matches a non-white space character.. – matches any character. 24 Regular Expressions Metacharacters Metacharacters are characters with a special meaning: Metacharacter Description. Find a single character, except newline or line terminator \w Find a word character \W Find a non-word character ARUL VIT SCOPE \d Find a digit \D Find a non-digit character \s Find a whitespace character \S Find a non-whitespace character \b Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b 25 Regular Expressions Metacharacters Metacharacters are characters with a special meaning: \B Find a match, but not at the beginning/end of a word \0 Find a NULL character \n Find a new line character \f Find a form feed character ARUL VIT SCOPE \r Find a carriage return character \t Find a tab character \v Find a vertical tab character \xxx Find the character specified by an octal number xxx \xdd Find the character specified by a hexadecimal number dd \udddd Find the Unicode character specified by a hexadecimal number dddd 26 Regular Expressions Quantifiers Quantifier Description n+ Matches any string that contains at least one n n* Matches any string that contains zero or more occurrences of n n? Matches any string that contains zero or one occurrences of n ARUL VIT SCOPE n{X} Matches any string that contains a sequence of X n's n{X,Y} Matches any string that contains a sequence of X to Y n's n{X,} Matches any string that contains a sequence of at least X n's n$ Matches any string with n at the end of it ^n Matches any string with n at the beginning of it ?=n Matches any string that is followed by a specific string n 27 ?!n Matches any string that is NOT followed by a specific string n * - Star The star symbol * matches zero or more occurrences of the pattern left to it. Expression String Matched? mn 1 match man 1 match mann 1 match ma*n No match (a is main not followed by n) woman 1 match 28 * + - Plus The plus symbol + matches one or more occurrences of the pattern left to it. Expression String Matched? mn No match (no a character) man 1 match mann 1 match ma+n No match (a is not followed main by n) woman 1 match 30 + ? - Question Mark The question mark symbol ? matches zero or one occurrence of the pattern left to it. Expression String Matched? mn 1 match man 1 match No match (more than maan ma?n one a character) No match (a is not main followed by n) woman 1 match 32 {} - Braces Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it. Expression String Matched? abc dat No match abc daat 1 match (at daat) a{2,3} 2 matches (at aabc aabc daaat and daaat) 2 matches (at aabc aabc daaaat and daaaat) 34 {} - Braces Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more than 4 digits. Expression String Matched? 1 match (match at ab123csde ab123csde) [0-9]{2,4} 12 and 345673 3 matches (12, 3456, 73) 1 and 2 No match 35 ^ - Caret The caret symbol ^ is used to check if a string starts with a certain character. Expression String Matched? a 1 match ^a abc 1 match bac No match abc 1 match ^ab No match (starts acb with a but not followed by b) 36 $ - Dollar The dollar symbol $ is used to check if a string ends with a certain character. Expression String Matched? a 1 match a$ formula 1 match cab No match 37 ?= ?! Regular Expressions Quantifiers Quantifier Description n+ Matches any string that contains at least one n n* Matches any string that contains zero or more occurrences of n n? Matches any string that contains zero or one occurrences of n ARUL VIT SCOPE n{X} Matches any string that contains a sequence of X n's n{X,Y} Matches any string that contains a sequence of X to Y n's n{X,} Matches any string that contains a sequence of at least X n's n$ Matches any string with n at the end of it ^n Matches any string with n at the beginning of it ?=n Matches any string that is followed by a specific string n 40 Using Regular Expressions in JavaScript Method Description Executes a search for a match in a string. It returns an array of exec() information or null on a mismatch. test() Tests for a match in a string. It returns true or false. Returns an array containing all of the matches, including capturing match() groups, or null if no match is found. Returns an iterator containing all of the matches, including matchAll() capturing groups. Tests for a match in a string. It returns the index of the match, or - search() 1 if the search fails. Executes a search for a match in a string, and replaces the matched replace() substring with a replacement substring. Executes a search for all matches in a string, and replaces the replaceAll() matched substrings with a replacement substring. Uses a regular expression or a fixed string to break a string into an split() array of substrings. 41 Regular Expression Methods The test() method The test() method compares the target text with the regex pattern and returns a boolean value accordingly. If there is a match, it returns true, otherwise it returns false. The exec() method The exec() method compares the target text with the regex pattern. If there's a match, it returns an array with the match – otherwise it returns null. For example: Regular Expressions 44 What are Regular Expression Flags? Flags or modifiers are characters that enable advanced search features including case- insensitive and global searching. You can use them individually or collectively. Some commonly used ones are: – g is used for global search which means the search will not return after the first match. – i is used for case-insensitive search meaning that a match can occur regardless of the casing. – m is used for multiline search. – u is used for Unicode search. 45 Regular Expression Modifier 46 Validating the Email Addresas 47 Regex Examples First example: How to use a regex pattern to match an email address: Regex Examples Let's interpret the pattern. Here's what's happening: – / represents the start of the regular expression pattern. – ^ checks for the start of a line with the characters in the character class. – [(\w\d\W)+ ]+ matches any word, digit and non-word character in the character class at least once. Notice how the parentheses were used to group the characters before adding the quantifier. This is same as this [\w+\d+\W+]+. – @ matches the literal @ in the email format. – [\w+]+ matches any word character in this character class at least once. – \. escapes the dot so it appears as a literal character. – [\w+]+$ matches any word character in this class. Also this character class is anchored at the end of the line. – / - ends the pattern Regex Examples Alright, next example: how to match a URL with format http://example.com or https://www.example.com: Let's also interpret this pattern. Here's what's happening: /...../ represents the start and end of the regex pattern ^ asserts for the start of the line [https?]+ matches the characters listed at least once, however ? makes 's' optional. : matches a literal semi-colon. \/\/ escapes the two forward slashes. (w{3}\.) matches the character w 3 times and the dot that follows immediately. However, this group is optional. [\w+]+ matches character in this class at least once. \. escapes the dot [\w+]+$ matches any word character in this class. Also this character class is anchored at the end of the line. Example 3: Validating the Phone Number 52 Regular Expressions JavaScript code to validate an email id function ValidateEmail(mail) { if (/^\w+([\.-]?\w+)*@\w+([\.- ]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)) { ARUL VIT SCOPE return (true) } alert("You have entered an invalid email address!") return (false) } 53 Regular Expressions To check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/; ARUL VIT SCOPE 54

Use Quizgecko on...
Browser
Browser