• code

    Regular Expressions Extract Matches

    So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the .match() method. To use the .match() method, apply the method on a string and pass in the regex inside the parentheses. Here’s an example: Here the first match would return ["Hello"] and the second would return ["expressions"]. Note that the .match syntax is the “opposite” of the .test method you have been using thus far:

  • code

    Regular Expressions Cheat Sheet

    Operator Code Example Or | /yes|no/let testStr = "Pumpkin"; let testRegex = /P(engu|umpk)in/; testRegex.test(testStr); Ignore Case i /Matt/i Extract Multiple Instances g /Repeat/g Wildcard (one character) . /hu./ Character Classes [ ] /b[aiu]g/ Range [-] /[a-e]at//[a-z0-9]/ Avoid Match [^] /[^aeiou]/ One or More Matches + /a+/ Zero or More Matches * /go*/ Lazy Match (smallest result) ? /<.*?>/ Beginning of String ^ /^Ricky/ Ending of String $ /story$/ Match All Numbers/Letters/Underscore \w /\w+//[A-Za-z0-9_]+/ ~Match All Numbers/Letters/Underscore \W Match All Numbers \d Match All Non-Numbers \D Match Whitespace \s Match Non-Whitespace \S Specify Number of Matches {#,#} a{2,3}ha{2,}ha{2}h Optional Match ? /colou?r/ Positive Lookahead (?=...) let quRegex = /q(?=u)/; Negative Lookahead…

  • code

    Regular Expressions Test Method

    Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching. If you want to find the word the in the string The dog chased the cat, you could use the following regular expression: /the/. Notice that quote marks are not required within the regular expression. JavaScript has multiple ways to use regexes. One way to test a regex is using the .test() method. The .test() method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not. The test method here returns true. In the last challenge, you searched for the word Hello using the regular expression /Hello/. That regex searched for…