4668 posts Love & Light
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Interviewed on an Envato blog
  • Made it to the Authors' Hall of Fame
+8 more
digitalscience says

How do you check if a string contains http:// or not? Thanks :)

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says
How do you check if a string contains http:// or not? Thanks :)
if (myString.split("http://").length > 1) { // is an url... }

:-)

6025 posts
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+8 more
VF says

Or use indexOf() to get same result:

if (myString.indexOf("http://")>-1) {
    trace("This string has a link");
}
3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

And to be sure that the string starts with “http://”


if (myLink.split("http://")[0] == "http://") {}

or

if (myLink.indexOf("http://") == 0) {}

:-)

4668 posts Love & Light
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Interviewed on an Envato blog
  • Made it to the Authors' Hall of Fame
+8 more
digitalscience says

ok thanks!

so what is length > 1… the length of the remaining bit where it split the string at “http://” ?

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says
ok thanks! so what is length > 1… the length of the remaining bit where it split “http://” ?
The split function returns an array, so if you have:

myString = "a,b,c,d,e";

if you do 

myString.split(",") you will get an array of 5 elements, so the length will be 5

if you do

myString.split("hello") the length of the array will be 0 because the separator won't be found
<pre /></pre>
3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

So basically if the url is valid you will get with the split an array with 2 elements.

At the index 0 you will have the “http://” and at the index 1 you will have the other part of the url.

861 posts
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Interviewed on an Envato blog
+5 more
MoeHaydar says

Intresting, I used to use VF’s method IndexOf but ParkerAndKent method is more advanced thanks

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says
Intresting, I used to use VF’s method IndexOf but ParkerAndKent method is more advanced thanks

Thanks, but it isn’t more advanced… we just use two functions that have different purposes but that give the same result in a case like this one ;-)

926 posts
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+4 more
michelsteege says

I also use indexOf split creates a array and you don’t need a array for this :)

by
by
by
by
by
by