<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Martin Wheeler</title>
    <description>The latest articles on DEV Community by Martin Wheeler (@martinwheeler).</description>
    <link>https://dev.to/martinwheeler</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202554%2Faa3e7e62-c8c0-403f-b8a4-f34749748280.png</url>
      <title>DEV Community: Martin Wheeler</title>
      <link>https://dev.to/martinwheeler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martinwheeler"/>
    <language>en</language>
    <item>
      <title>TIL: RegExp global flag doesn't return what you'd expect</title>
      <dc:creator>Martin Wheeler</dc:creator>
      <pubDate>Mon, 16 Sep 2019 13:12:51 +0000</pubDate>
      <link>https://dev.to/martinwheeler/til-regexp-global-flag-doesn-t-return-what-you-d-expect-3cc5</link>
      <guid>https://dev.to/martinwheeler/til-regexp-global-flag-doesn-t-return-what-you-d-expect-3cc5</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myRegExp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+ &lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+ &lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+/gi&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10 Banana Road&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doesItMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myRegExp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;itShouldMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myRegExp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Wait, what?!? Why is it &lt;code&gt;true&lt;/code&gt; and then &lt;code&gt;false&lt;/code&gt;? What's going on here?&lt;/p&gt;

&lt;p&gt;The answer isn't as intuitive as I first thought. This is what I found on stackoverflow: &lt;a href="https://stackoverflow.com/a/1520853"&gt;https://stackoverflow.com/a/1520853&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Essentially the global flag is what's causing the issue. It appears to have some internal state which wasn't something I was aware of. Upon removing the global flag the tests will both return &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
