{"id":187,"date":"2010-02-13T16:20:19","date_gmt":"2010-02-13T23:20:19","guid":{"rendered":"http:\/\/kai.mactane.org\/blog\/?p=187"},"modified":"2011-07-04T10:27:44","modified_gmt":"2011-07-04T17:27:44","slug":"silly-coding-tricks-inverted-string-match","status":"publish","type":"post","link":"https:\/\/kagan.mactane.org\/blog\/2010\/02\/13\/silly-coding-tricks-inverted-string-match\/","title":{"rendered":"Silly Coding Tricks: &#8220;Inverted&#8221; String Match"},"content":{"rendered":"<p>First things first: <strong>Never actually do this.<\/strong> This is just a fun curiosity, for amusement value only.<\/p>\n<p>Because of the way JavaScript&#8217;s <code>search()<\/code> method works, you can do:<\/p>\n<div class=\"code\">var my_url = &apos;http:\/\/kai.mactane.org&apos;;<br \/>\nif (! my_url.search(\/http:\\\/\\\/kai.mactane.org\/)) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Your URL is http:\/\/kai.mactane.org&quot;);<br \/>\n} else {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Nope, your URL isn&apos;t http:\/\/kai.mactane.org&quot;);<br \/>\n}<\/div>\n<p>Try running this, and it will correctly claim that &#8220;Your URL is http:\/\/kai.mactane.org&#8221;&nbsp;&mdash; even though there&#8217;s a ! at the beginning of the test. What gives?<\/p>\n<p>That exclamation point <em>looks<\/em> like it&#8217;s saying: &#8220;If the search for <code>http:\/\/kai.mactane.org<\/code> within the given string fails&#8230;&#8221; But what that test is <em>really<\/em> saying is: &#8220;If the search for <code>http:\/\/kai.mactane.org<\/code> returns Boolean false&nbsp;&mdash; or <em>anything that evaluates as<\/em> Boolean false&#8230;&#8221;<\/p>\n<p>What that <code>search()<\/code> call actually returns is <em>the character index<\/em> at which the needle is found, or -1 if it isn&#8217;t found. Since the needle is found at the very beginning of the haystack&nbsp;&mdash; at character index 0&nbsp;&mdash; <code>search()<\/code> returns zero. Which evaluates to false in a Boolean context.<\/p>\n<p>If the needle isn&#8217;t found, the search will return -1&nbsp;&mdash; which <em>evaluates as true<\/em> in a Boolean context!<\/p>\n<p>Effectively, the ! operator is reconciling the disagreement between <code>search()<\/code>&rsquo;s idea of falsehood and <code>if()<\/code>&rsquo;s idea of falsehood: <code>search()<\/code> returns -1 for a false result (a failed match), but <code>if()<\/code> considers -1 to be true, not false.<\/p>\n<p>This trick only works with needles that you are sure will only occur at the beginning of the haystack.<\/p>\n<p>Once again, you should <em>never, ever <strong>actually<\/strong> write code like this<\/em> for any serious purpose or in anything you intend to deploy in the real world. The maintainability problems are not worth the amusement of confusing all your co-workers with a test that looks reversed. Instead, use a positional anchor in your regex, and explicitly test against -1&#8230;<\/p>\n<div class=\"code\">if (my_url.search(\/^http:\\\/\\\/kai.mactane.org) != -1)<\/div>\n<p>&#8230;like any sensible JavaScript coder. (Yes, that last bit is intended to be ironic. I wrote something once about the silliness of having to add that extra test; I&#8217;ll have to see if I can find it and republish it here. )<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First things first: Never actually do this. This is just a fun curiosity, for amusement value only. Because of the way JavaScript&#8217;s search() method works, you can do: var my_url = &apos;http:\/\/kai.mactane.org&apos;; if (! my_url.search(\/http:\\\/\\\/kai.mactane.org\/)) { &nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Your URL is http:\/\/kai.mactane.org&quot;); } else { &nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Nope, your URL isn&apos;t http:\/\/kai.mactane.org&quot;); } Try running this, and it will [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[63,7,83,82],"_links":{"self":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/187"}],"collection":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":7,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}