Javascript Regultar Expression for matching Domains on a string
 

Today I had a hard time creating a regexp to match domains on a string. I wouldn't like the internauts who visit this small blog to suffer the same pain, so here's how to :

var StringWithData = " asd http://subdomain.perrohunter.com/lol/something.php asd";
re = /http://w+([.-]?[a-z]+)*.com/;
var results = re.exec(StringWithData);
alert(results[0]);

results will be an array of matches so if you only want the first match use the 0 position, else loop the array for what you need.

 

Comments

There are no comments

Post a comment