Regex check hostname in a string

Hi,
I’ve to check if a string contains the hostname
my 5 cents and it seems to work but not being at all
a regex guru … :slight_smile:


var re = new RegExp(window.location.hostname,'i');
alert(re.test(str));

str can be like:

http://www.MYdomain.com/
www.mydomain.com/action/id/12
www.mydomain.com?id=12
sub.mydomain.com

and so on …

Hi,

That does work.


var l = window.location.hostname;
var regex = new RegExp(l, 'i');

var options = [
	'http://www.mydomain.com/',
	'http://www.mydomain.com',
	'www.mydomain.com/',
	'http://www.MYdomain.com/',
	'www.mydomain.com/action/id/12',
	'www.mydomain.com?id=12',
	'sub.mydomain.com'
]
for (var i=0, ii=options.length; i<ii; i++) {
	alert(regex.test(options[i]));
}

Thanks everybody for the reply
@markbrown4 I wanted be sure :slight_smile:
@Stomme_poes
var l = window.location.hostname;

I had no idea what you were talking about, so I had written a regex : )

[ot]
Man I’m already getting bogged on this tagging thing. Trying to find my mail just got harder.[/ot]

Off Topic
http://youtu.be/du2hzpMUxQQ :slight_smile: