title: Check if a string starts with http using Javascript
body: <p>I've been searching all over for an answer to this and all of the answers I've found haven't been in JavaScript.</p>
  
  <p>I need a way, in javascript, to check if a string starts with http, https, or ftp. If it doesn't start with one of those I need to prepend the string with <code>http://</code>. indexOf won't work for me I don't think as I need either http, https or ftp. Also I don't want something like <code>google.com/?q=http://google.com</code> to trigger that as being valid as it doesn't start with an http whereas indexOf would trigger that as being true (if I'm not entirely mistaken).</p>
  
  <p>The closest PHP regex I've found is this:</p>
  
  <pre><code>function addhttp($url) {
     if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
     }
     return $url;
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/2762061/how-to-add-http-if-its-not-exists-in-the-url">Source: How to add http if its not exists in the url</a> </p>
  
  <p>I just don't know how to convert that to javascript. Any help would be greatly appreciated.</p>