Closed Thread Icon

Preserved Topic: Regular Expressions (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17859" title="Pages that link to Preserved Topic: Regular Expressions (Page 1 of 1)" rel="nofollow" >Preserved Topic: Regular Expressions <span class="small">(Page 1 of 1)</span>\

 
Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 09-14-2000 20:09

(I posted this on the Gurus board to, so apologies to those who read both)
I'm annoyed. I want to write a special welcome line in my home page if the visitor came from Gurusnetwork. I wrote:

if (document.referrer=="http://www.gurusnetwork.com") {
document.write("<P>Hey - thanks for visiting from the GurusNetwork. I\'m flattered blah blah bah.<\/P>")

and it worked! Then I noticed people came from the /associates/ page, and it didn't work, so I added an

else if (document.referrer=="http://www.gurusnetwork.com\/associates\/")

and it worked. THEN ... I noticed people came from the individual tute page and it didn't work. Clearly I need a regular expression to get anything following the ".com", but I can't figure it out.

Right now I have

if (document.referrer==/http:\/\/www.gurusnetwork.com\S*/i) {
document.write("<P>Hey - thanks for visiting from the GurusNetwork. I\'m flattered blah blah blah.<\/P>")
}

and it doesn't work. Any RegEx whizzes want to enlighten me?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-14-2000 21:14

Well, my JavaScript book covers them, but I'm no expert so I'll wait for a Perl guru to come along first...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-14-2000 21:15

Okay I am not a big fan of regular expressions (My bad but I need to learn more)

Try this.

if (document.referrer,indexOf('www.gurusnetwork.com') >= 0){
document.write('blah blah blah');
}


Alright its not a regular expression but it should work. If the referrer's URL has "www.gurusnetwork.com" anywhere in the URL then it returns true


Walking the Earth like Kane

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 09-14-2000 21:25

Thanks. It didn't HAVE to be a RegEx, just couldn't think of any other way. This looks cleaner and simpler. RegEx is so tough for me to wrap my brain around, but when I get one to work it is so fast and powerful I jump up and down.

Will try this now. Thanks!!

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 09-14-2000 21:42

Fooey.

When I type

<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
<!-- hide
if (document.referrer,indexOf('www.gurusnetwork.com') >= 0){
document.write("<P>Hey - thanks for visiting from the GurusNetwork. I\'m flattered you followed the link to my site, and hope you will find something helpful here.<\/P>")
}
// end hiding-->
</SCRIPT>

The debugger says

indexOf is not defined. and it doesn't work either.

Das
Maniac (V) Inmate

From: Houston(ish) Texas
Insane since: Jul 2000

posted posted 09-14-2000 22:01

The regular expression:
Try:
if(document.referrer=="http:\/\/www.gurusnetwork.com\/.*")
Mind you, I've never used regular expressions in JavaScript; I'm going from my old UNIX days. The '.' matches any character, the 'S' only matches some nonspace characters.

Edit: Looking through my JavaScript reference, I don't think you can use the '==' operator that way. Use the Indexof method <img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">


The indexof probably isn't working because you've got a comma instead of a period after document.referrer (bitdamaged made a typo). Try:
if(document.referrer.indexOf('www.gurusnetwork.com') > -1) ...



[This message has been edited by Das (edited 14-09-2000).]

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 09-14-2000 22:26

Das - your eyes caught it. Sheesh. Copy and paste is a wonderful thing, but it helps to actually look at it too.

So bitdamaged got the solution and Das caught the booboo, and now it works just lovely.

Thanks all!



[This message has been edited by Steve (edited 14-09-2000).]

Das
Maniac (V) Inmate

From: Houston(ish) Texas
Insane since: Jul 2000

posted posted 09-14-2000 23:13

I'm a C++ programmer by trade; I'm very skilled at finding little bitty punctuation errors <img border=0 align=absmiddle src="http://www.ozones.com/forum/smile.gif">

Glad it's working for you, now.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-15-2000 00:02

Ah, glad you got it.

You might consider using "!= -1" instead of ">= 0"... the function returns negative one if it doesn't find it, or the position if it does find it. Obviously, both will work, so don't worry about it, but I've seen more people use "!= -1"... it seems to be more universally accepted.

Again, it really doesn't matter.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-15-2000 02:18

Actually Slime is right I think you do want to use the !=-1 or actually just < 0

I believe the first position is 0 when index of is used.


Walking the Earth like Kane

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-15-2000 03:32

Well, don't use "< 0" because that will return true if it *isn't* there. Either "!= -1" or ">= 0" will work.

And the first position is zero, as bitdamaged said.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-15-2000 04:18

yeah
>0

either way thats what I meant

thanks Slime!


Walking the Earth like Kane

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-15-2000 04:19

=D

Steve
Maniac (V) Inmate

From: Boston, MA, USA
Insane since: Apr 2000

posted posted 09-15-2000 21:56

They both worked for me. I switched to the !=-1 since Slime said it's more commonly seen and who wants to be uncommon, but for the record both approaches worked well.
Thanks
And thanks.

« BackwardsOnwards »

Show Forum Drop Down Menu