4509 posts Love & Light
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 7-8 years
  • Author had a File in an Envato Bundle
  • Most Wanted Bounty Winner
  • Interviewed on the Envato Notes blog
  • South Africa
+3 more
digitalscience says

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

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says
How do you check if a string contains http:// or not? Thanks :)
if (myString.split("http://").length > 1) { // is an url... }

:-)

5766 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Elite Author
  • Has been a member for 6-7 years
  • Sold between 100 000 and 250 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • India
+3 more
VF says

Or use indexOf() to get same result:

if (myString.indexOf("http://")>-1) {
    trace("This string has a link");
}
3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 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) {}

:-)

4509 posts Love & Light
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 7-8 years
  • Author had a File in an Envato Bundle
  • Most Wanted Bounty Winner
  • Interviewed on the Envato Notes blog
  • South Africa
+3 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
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 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
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 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.

848 posts
  • Football Contest Participant/Runner-up
  • Referred between 1 and 9 users
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Lebanon
  • Exclusive Author
MoeHaydar says

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

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 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 ;-)

925 posts
  • Has been a member for 7-8 years
  • Beta Tester
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Referred between 50 and 99 users
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