5

I want to be able to check a string to see if it has http:// at the start and if not to add it.

if (regex expression){
string = "http://"+string;
}

Does anyone know the regex expression to use?

irl_irl
  • 3,097
  • 8
  • 44
  • 56

9 Answers9

51

If you don't need a regex to do this (depending on what language you're using), you could simply look at the initial characters of your string. For example:

// C#
if (!str.StartsWith("http://"))
    str = "http://" + str;

// Java
if (!str.startsWith("http://"))
    str = "http://" + str;

// JavaScript/TypeScript
if (str.substring(0, 7) !== 'http://')
    str = 'http://' + str;
David R Tribble
  • 10,646
  • 4
  • 39
  • 50
8

Should be:

/^http:\/\//

And remember to use this with ! or not (you didn't say which programming language), since you are looking for items which don't match.

Adam Bellaire
  • 99,441
  • 19
  • 144
  • 160
7

In JavaScript:

if(!(/^http:\/\//.test(url)))
{
    string = "http://" + string;
}
Chris Doggett
  • 17,776
  • 4
  • 55
  • 84
5
var url = "http://abcd";
var pattern = /^((http|https|ftp):\/\/)/;

if(!pattern.test(url)) {
    url = "http://" + url;
}

alert(url);
Deepti Gehlot
  • 571
  • 6
  • 7
  • 1
    While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. - [From Review](http://stackoverflow.com/review/low-quality-posts/13275385) – Michael Parker Aug 09 '16 at 16:16
  • Thank you so much! It helps me improve in processing – Blue Tram Feb 23 '19 at 07:26
3

Something like this should work ^(https?://)

Jignesh Rajput
  • 3,480
  • 25
  • 50
Michael Ciba
  • 551
  • 3
  • 6
2
yourString = yourString.StartWith("http://") ? yourString : "http://" + yourString

Is more sexy

Nicolas Dorier
  • 7,257
  • 11
  • 54
  • 72
1
 /^http:\/\//
scragar
  • 6,468
  • 24
  • 33
0

If javascript is the language needed here, then look at this post which adds "startswith" property to the string type.

Community
  • 1
  • 1
vsync
  • 87,559
  • 45
  • 247
  • 317
0

For me, with PHP these are the 2 I use just adding them here for completeness.

$__regex_url_no_http = "@[-a-zA-Z0-9\@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()\@:%_\+.~#?&//=]*)@";

$__regex_url_http = "@https?:\/\/(www\.)?[-a-zA-Z0-9\@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()\@:%_\+.~#?&//=]*)@";

I have a function like this to check:

/**
 * Filters a url @param url If @param protocol is true
 * then it will check if the url contains the protocol
 * portion in the url if it doesn't then false will be
 * returned.
 * 
 * @param string $url
 * @param boolean $protocol
 * @return boolean
 */   
public function filter_url($url, $protocol=false){
  $response = FALSE;

  $regex = $protocol == false ? $this->__regex_url_no_http:$this->__regex_url_http;

  if(preg_match($regex, $url)){
    $response = TRUE;
  }

  return $response;
}

I didn't create the regex. I found them someplace but seem to be compliant

Kyle Coots
  • 1,881
  • 1
  • 16
  • 23