<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wendy Chang on Medium]]></title>
        <description><![CDATA[Stories by Wendy Chang on Medium]]></description>
        <link>https://medium.com/@wendy.changw?source=rss-485a11a1ffa0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*X4mmnX0fcXVPP7A2NzNtKw.jpeg</url>
            <title>Stories by Wendy Chang on Medium</title>
            <link>https://medium.com/@wendy.changw?source=rss-485a11a1ffa0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:39:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wendy.changw/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adventures in Regular Expression — JS Edition]]></title>
            <link>https://medium.com/@wendy.changw/adventures-in-regular-expression-js-edition-477fc86c2878?source=rss-485a11a1ffa0------2</link>
            <guid isPermaLink="false">https://medium.com/p/477fc86c2878</guid>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[regular-expressions]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Wendy Chang]]></dc:creator>
            <pubDate>Sat, 14 Apr 2018 12:39:43 GMT</pubDate>
            <atom:updated>2018-05-08T17:01:39.593Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*cNied1kpXyBRp4GwW7Jn8w.png" /></figure><h3>// What is Regular Expression?</h3><p>Regular Expression, commonly referred to as regex, is a powerful string matcher. It has multiple fields of application ranging from string parsing to database queries, data validation, syntax highlighting, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*uqqYN79AucSNMypPKB392g.jpeg" /><figcaption>A very real example from the MDN docs</figcaption></figure><p>To those unfamiliar with regular expression, it can look like an intimidating line of gibberish. Don’t let looks deceive you though, because once you get the hang of it you’ll discover regex is a gift that keeps giving and can save you tons of time!</p><p>Note: I’ll be providing a number of examples throughout this post, you can test those out and create your own regular expressions at <a href="https://regexr.com/">regexr.com</a>.</p><h3>// The basics:</h3><p><strong>— How it works</strong></p><p>/ab+c/</p><p>Regular expression begins and ends with a forward slash with the pattern you’re looking for delimited by the two slashes. For example, /ab+c/ will match the sequence of characters for “ab” and “c” or “abc”.</p><p><strong>— Methods</strong></p><p>In JavaScript, regular expression is an object and as such there are a number of methods you can use with it:</p><ul><li>test — Returns a boolean. True if the string contains a match of the regex pattern and false otherwise.</li><li>exec —Returns an array of information or null on a mismatch.</li><li>You can also use regex on String methods like <strong>match</strong> (similar to exec), <strong>replace</strong>, <strong>search</strong>, and <strong>split</strong>.</li></ul><h3>// Syntax</h3><p>The key to using regular expression is understanding its characters and syntax. Once you have a good grasp on those, the once esoteric symbols become decipherable!</p><p><strong>— Metacharacters</strong></p><p>Be sure to familiarize yourself with the cast of metacharacters since you’ll be using them to compose regular expressions!</p><p><strong>[ { ( ) \ ^ $ . | ? * +</strong></p><ul><li>Metacharacters have special meanings so to use them literally you’ll need to escape them with a backslash. For example, the regex for “hello?” is /hello\?/</li></ul><p><strong>— Anchors</strong></p><p>Anchors are used to specify positions for the regex match.</p><ul><li><strong>^</strong> — beginning of string</li><li><strong>$ </strong>— end of string</li></ul><pre><strong>&quot;abc&quot;.match(/^a/)</strong>     Matches &quot;a&quot;</pre><pre><strong>&quot;abc&quot;.match(/^b/)     </strong>No match found because the string doesn&#39;t contain a word with &quot;b&quot; at the starting position.</pre><p><strong>— Flags</strong></p><p>Regular expressions have optional flags that can be used separately or together in any order. They are placed after the ending slash like so /ab+c/gi. You’ll likely encounter the first three in the list most often.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab2aa57cb2d14e6778f5f679c1fe4c55/href">https://medium.com/media/ab2aa57cb2d14e6778f5f679c1fe4c55/href</a></iframe><p><strong>— Character classes</strong></p><p>Character classes will match a single character from a set of characters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75c48f5da8c9dfd508579dae6d20168f/href">https://medium.com/media/75c48f5da8c9dfd508579dae6d20168f/href</a></iframe><p>Examples:</p><pre><strong>\w\d          </strong>Matches a single alphanumeric character followed <br>              immediately by a digit character</pre><pre><strong>[a-c]         </strong>Matches &quot;a&quot;, &quot;b&quot;, or &quot;c&quot;. Equivalent to [abc]</pre><ul><li>Most metacharacters don’t need to be escaped inside brackets</li></ul><pre><strong>[.\s$*]         </strong>Matches &quot;.&quot;, whitespace, &quot;$&quot;, or &quot;*&quot;</pre><ul><li>You can combine predefined character classes to form more complex character classes:</li></ul><pre><strong>col[ou]r\s     </strong>Matches &quot;color &quot; or &quot;colur &quot;. Notice it will not <br>               match &quot;colour &quot; or &quot;coluor &quot; because only a single <br>               character inside the brackets is matched.</pre><ul><li>The uppercase form of the predefined character class (i.e. \W, \D, \S) is usually the negated version of its lowercase self (\w, \d, \s).</li></ul><p><strong>— Grouping and capturing</strong></p><p>You can group regular expressions together by placing them inside parentheses. This enables you to apply a quantifier to the entire group or use alternation on just part of the regex.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ef399e76e4312e896ce2ec574209bd0/href">https://medium.com/media/8ef399e76e4312e896ce2ec574209bd0/href</a></iframe><ul><li>Examples:</li></ul><pre><strong>d(og) </strong>     Captures the group &quot;og&quot;<br><strong>d(?:og)</strong>    Non-capture group &quot;og&quot;<br><strong>d(?=og)</strong>    Matches &quot;d&quot; only if it is followed by &quot;og&quot;<br><strong>d(?!og)</strong>    Matches &quot;d&quot; only if it is NOT followed by &quot;og&quot;</pre><ul><li>You can incorporate alternatives into the match for a wider range. And alternatives can be empty!</li></ul><pre><strong>(d|bl)og</strong>    Matches &quot;d&quot; or &quot;bl&quot; followed by &quot;og&quot;</pre><pre><strong>(d|bl|)og</strong>   Same as above except now it also matches &quot;og&quot; by itself</pre><ul><li>Order matters! Regex will execute the match based on the order specified.</li></ul><pre><strong>do(|g)</strong>      Matches &quot;do&quot;, but not &quot;dog&quot;. This is because matches do <br>            not overlap. <br><strong>do(g|)</strong>      Matches &quot;dog&quot; or &quot;do&quot;</pre><p><strong>— Quantifiers</strong></p><p>Quantifiers * + {} are used to specify how many times you want the pattern to be matched.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c7b6af8094a2b7149684719779addb2/href">https://medium.com/media/2c7b6af8094a2b7149684719779addb2/href</a></iframe><ul><li>Examples:</li></ul><pre><strong>abc*</strong>        Matches a string &quot;ab&quot; followed by zero or more &quot;c&quot;s <br><strong>abc+</strong>        Matches a string &quot;ab&quot; followed by one or more &quot;c&quot;s <br><strong>abc?</strong>        Matches a string &quot;ab&quot; followed zero or one &quot;c&quot;<br><strong>abc{3} </strong>     Matches a string &quot;ab&quot; followed by 3 &quot;c&quot;s<br><strong>abc{3,}</strong>     Matches a string &quot;ab&quot; followed by &quot;c&quot; 3 or more times <br><strong>abc{3,5}</strong>    Matches a string &quot;ab&quot; followed by &quot;c&quot; 3 to 5 times <br><strong>(abc){1,3}</strong>  Matches a string group &quot;abc&quot; 1-3 times<br><strong>a(bc)+ </strong>     Matches a string &quot;a&quot; once followed by the group &quot;ab&quot; one   <br>            or more times.</pre><p>One thing to be aware of with quantifiers is that they’re greedy and will try to grab as much of the content as they can. For example, &lt;.+&gt; will match everything between the first opening tag and the last closing tag:</p><pre><strong>&quot;&lt;div&gt;hello&lt;/div&gt;&quot;.match(/</strong><strong>&lt;.+&gt;/g) </strong>results in <strong>[&quot;&lt;div&gt;hello&lt;/div&gt;&quot;]</strong></pre><p>To remedy this, you can lazify the match using a ?, which will cause the match to stop after the minimum requirement is satisfied. In this case, it will match only what’s between the opening and closing tags:</p><pre><strong>&quot;&lt;div&gt;hello&lt;/div&gt;&quot;.match(/</strong><strong>&lt;.+?&gt;/g) </strong>results in <strong>[&quot;&lt;div&gt;&quot;, &quot;&lt;div&gt;&quot;]</strong></pre><p><strong>— Assertions / Boundaries</strong></p><p>\b is an anchor-like marker that matches the positions of the word boundary. This can be the beginning or end of the string, or any point in the string that has a word character (\w) on one side and a non-word character on the other.</p><pre><strong>\bfoo\b     </strong>Matches &quot;foo&quot; but only if it&#39;s NOT surrounded by word characters on either side. So &quot;food&quot; doesn&#39;t contain a match but it&#39;ll match &quot;foo&quot; in &quot;&lt;p&gt;foo&lt;/p&gt;&quot;.</pre><p>Its negation, \B, on the other hand, will match all positions where \b doesn’t match.</p><pre><strong>\Bfoo\B      </strong>Matches &quot;foo&quot; only if it&#39;s surrounded by word characters, i.e. will match &quot;foo&quot; in &quot;deliciousfood&quot;.</pre><p>You can mix and match the two!</p><pre><strong>\bfoo\B      </strong>Matches &quot;foo&quot; if it&#39;s not surrounded by a word character on the left and surrounded by a word character on the right, i.e. matches &quot;foo&quot; in &quot;food&quot;.</pre><h3>// Practice problems</h3><p>Now that you have the basics of regular expressions down, let’s try our hand at some exercises!</p><ol><li>Create a regex pattern that matches hex colors. <br>I.e. #c0ded, #5eabed, #c0ffee</li><li>Write a regex that checks whether a given string represents a valid domain address.<br>I.e. <a href="http://www.helloregex.com,/">www.helloregex.com,</a> www.regexrocks.co.uk</li><li>Write a function that uses regex to capitalize the first letter of every word in a string. <br>I.e. “i heart regex, it is super fun!” should output “I Heart Regex, It Is Super Fun!”</li></ol><p>After attempting the problems, you can check your answers in this <a href="https://gist.github.com/wwendyc/2ef7ff10f6a4dfb40bb3c31e278ebbb0">gist</a>!</p><h3>// Resources</h3><p>If you’re convinced of the usefulness of regular expressions and would like to dive deeper, here are some additional materials to get you started!</p><ul><li><a href="https://regexone.com/problem/matching_decimal_numbers">RegexOne </a>— Put your knowledge to the test with practice problems</li><li><a href="https://regexr.com/">RegExr</a> — An online tool to learn, build, &amp; test Regular Expressions</li><li><a href="https://www.youtube.com/watch?v=M7vDtxaD7ZU">Demystifying Regular Expressions </a>— Excellent presentation by Lea Verou</li><li><a href="https://eloquentjavascript.net/09_regexp.html">Eloquent JavaScript </a>— A whole chapter dedicated to Regular Expressions with practice problems at the end</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">MDN doc on Regular Expressions</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=477fc86c2878" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>