I am trying to create a JavaScript regular expression that can find a keyword in the referrers host name. It cannot be in the file path, just the host name. So… these should pass:
http://keyword.com/
http://www.keyword.com/
http://keyword.example.com/
http://examplekeyword.com/
But these should not:
http://example.com/keyword
http://example.com/?q=keyword
http://example.com/example/keyword.php
I also unsure how to create the regex. I’ve seen var re = new regex(‘actual regex’); and also mystring.search(‘actual regex’); . Any help would be greatly appreciated!
Will match if the keyword is present. What it returns is kinda odd (
http://keywordforhttp://keyword.com), but it’s the least complex you can do.If you’re interested in the full hostname being returned,
Returns
http://thekeywordexists.comforhttp://thekeywordexists.com/foo.There are a number of ways to use regular expressions in Javascript. Regular-Expressions.info’s Javascript page is likely a good resource for this. (Performance tip: create the RegExp object once and make it a variable, rather than creating it every time you use it :o)