{"id":2655,"date":"2019-04-22T10:11:59","date_gmt":"2019-04-22T16:11:59","guid":{"rendered":"http:\/\/alliwalk.com\/blog\/?p=2655"},"modified":"2019-04-22T10:11:59","modified_gmt":"2019-04-22T16:11:59","slug":"learning-regular-expressions-regex","status":"publish","type":"post","link":"https:\/\/alliwalk.com\/blog\/2019\/04\/learning-regular-expressions-regex\/","title":{"rendered":"Learning Regular Expressions (regex)"},"content":{"rendered":"<p>Last on my list of JavaScript education\u00a0was\u00a0most recently regular expressions, which are ways for a programmer to search for strings in text. I followed FreeCodeCamp&#8217;s (FCC) curriculum. You can find more about <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions\">Regular Expressions on MDN<\/a>.<\/p>\n<p>The short name for regular expressions is &#8220;regex&#8221;, or &#8220;regexp&#8221;. The basic outline of a &#8216;regex&#8217; goes like this, which tests if &#8220;Happy&#8221; is in <code>thisLine<\/code>:<\/p>\n<pre>let thisLine = \"Happy happy, joy joy!\"; \r\nlet thisMatch = \/Happy\/; \r\nlet outcome = thisMatch.test(thisLine); \r\nconsole.log(outcome);<\/pre>\n<p>The outcome is <code>true<\/code>. The\u00a0<code>test<\/code>\u00a0method only results in true or false.<\/p>\n<p>In addition to <code>test<\/code>, you might also use <code>match<\/code> and <code>replace<\/code>. I believe those are the only 3 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions#Working_with_regular_expressions\" target=\"_blank\" rel=\"noopener noreferrer\">methods<\/a> used in the FCC set of lessons.<\/p>\n<p>Let&#8217;s try match. If you wanted to match &#8220;Happy&#8221;, and only &#8220;Happy&#8221;, you&#8217;d write:<\/p>\n<pre>let thisLine = \"Happy happy, joy joy!\"; \r\nlet thisMatch = \/^H[a-z]+\/; \r\nlet outcome = thisLine.match(thisMatch); \r\nconsole.log(outcome);\r\n<\/pre>\n<p>The outcome is <code>[Happy]<\/code>. Probably the most important part of regex is using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions#Using_special_characters\" target=\"_blank\" rel=\"noopener noreferrer\">special characters<\/a>, and that&#8217;s what&#8217;s going on in this example. The special characters are shortcuts to help you search for a specific string. This examples searches for a string starting with a capital H (<code>^H<\/code>) and is following by any letter from a-z (<code>[a-z]<\/code>), and repeats the search for as long as necessary until reaching a break (like a space) (<code>+<\/code>).<\/p>\n<p>An even simpler way to write the above would be to use a special character (<code>\\w<\/code>) to search for any non-digit character and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Regular_Expressions#Advanced_searching_with_flags_2\">flags<\/a> to allow upper or lower case (<code>i<\/code>), and global to search the entire string (<code>g<\/code>).<\/p>\n<pre>let thisLine = \"Happy happy, joy joy!\"; \r\nlet thisMatch = \/^H\\w+\/ig; \r\nlet outcome = thisLine.match(thisMatch); \r\nconsole.log(outcome);<\/pre>\n<p>You can also return only non-alphanumeric or digit characters.<\/p>\n<pre>let thisLine = \"Happy happy, joy joy!\"; \r\nlet thisMatch = \/\\W+\/ig; \r\nlet outcome = thisLine.match(thisMatch); \r\nconsole.log(outcome);<\/pre>\n<p>The outcome is\u00a0<code><span class=\"pun\">[<\/span><span class=\"str\">\" \"<\/span><span class=\"pun\">,<\/span> <span class=\"str\">\", \"<\/span><span class=\"pun\">,<\/span> <span class=\"str\">\" \"<\/span><span class=\"pun\">,<\/span> <span class=\"str\">\"!\"<\/span><span class=\"pun\">]<\/span><\/code><\/p>\n<p>There are many more examples of special characters, and I&#8217;ll be honest in admitting that I find it confusing. It took me a little while to figure out the second example above, partly because MDN separates their special characters onto different lines. Try these out in <a href=\"https:\/\/jsbin.com\/?console\">jsbin<\/a>.<\/p>\n<p>Anyway, regex are good to know if you need it.<\/p>\n<hr \/>\n<p>From FreeCodeCamp:<\/p>\n<blockquote><p>Regular expressions are special strings that represent a search pattern. Also known as &#8220;regex&#8221; or &#8220;regexp&#8221;, they help programmers match, search, and replace text. Regular expressions can appear cryptic because a few characters have special meaning. The goal is to combine the symbols and text into a pattern that matches what you want, but only what you want. This section will cover the characters, a few shortcuts, and the common uses for writing regular expressions.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A very brief overview of regular expressions after finishing the lessons on FreeCodeCamp.org.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,343],"tags":[512,325,514,513,515],"_links":{"self":[{"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/posts\/2655"}],"collection":[{"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/comments?post=2655"}],"version-history":[{"count":6,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/posts\/2655\/revisions"}],"predecessor-version":[{"id":2662,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/posts\/2655\/revisions\/2662"}],"wp:attachment":[{"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/media?parent=2655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/categories?post=2655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alliwalk.com\/blog\/wp-json\/wp\/v2\/tags?post=2655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}