Extract domain name with RegExp


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // with protocol
  2. var url:String = "http://www.domain.com/?value=test&message=debug";
  3. var reg:RegExp = /([a-z]*:\/\/)?([a-z-_]*)?.?[a-z-_]*.[a-z]*/;
  4. trace(url.match(reg)[0]); // http://www.domain.com
  5.  
  6. // without base protocol
  7. var url:String = "http://www.domain.com/?value=test&message=debug";
  8. var reg:RegExp = /[^https:\/\/]([a-z-_]*)?.?[a-z-_]*.[a-z]*/;
  9. trace(url.match(reg)[0]); // www.domain.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.