Reputation: 115
Hey guys can you help me out? Right now, I have a code like this :
<script>
var ref = document.referrer;
if (ref.search(/^https?:\/\/([^\/]+\.)?website\.com(\/|$)/i))
{
alert("**...***");
window.location.href = "http://www.website.com";
}
</script>
It does track referrals from website.com but not from www.website.com thats why the code keeps on going again and again.
Can you help me out and teach me how to add a "www." regex?
Upvotes: 0
Views: 177
Reputation: 91488
According to documentation: The search
method returns
The index of the first match between the regular expression and the given string; if not found, -1.
You are testing true or false, but if the index is 0, it is equivalent to false.
Test for -1
value.
var ref = "http://www.website.com";
if (ref.search(/^https?:\/\/([^\/]+\.)?website\.com\/?$/i) !== -1 ) {
// here ___^^^^^^
console.log('Match');
} else {
console.log('No match');
}
Upvotes: 1
Reputation: 3631
First, there are tools like regexpal where you can check and test regexes online, also there are lots of docs there and in its equivalent's websites.
I'd try something like: ref.search(/^https?:\/\/([^\.]+\.)?website\.com(\/.*|$)?/i)
or ref.toLowerCase.indexOf('http://www.website.com') === 0
to check whether it certainly started like that (or not)...
Upvotes: 0
Reputation: 15
try this
(http(s)?://)?([\w-]+.)+[\w-]+(/[\w- ;,./?%&=]*)?
will match
www.google.co
www.google.com
google.com
google.co.in
googgle.Net
www.google.com
http://www.google.com
https//gogle.com
Upvotes: 0