如何使用正则表达式将:
若是虚拟目录:http://localhost/Store.WebSiteUI/Suggest.aspx 截取成http://localhost/
若是站点www.test.com/testweb/default.aspx 截取成www.test.com/总之:只要第一个/之前的内容(包括/)
要求:用正则,用string.substring+indexOf方式的兄弟们勿扰.....谢谢大家!
若是虚拟目录:http://localhost/Store.WebSiteUI/Suggest.aspx 截取成http://localhost/
若是站点www.test.com/testweb/default.aspx 截取成www.test.com/总之:只要第一个/之前的内容(包括/)
要求:用正则,用string.substring+indexOf方式的兄弟们勿扰.....谢谢大家!
/// <summary>
/// 提取InternetURL的域名(Domain)
/// </summary>
/// <param name="input">对其匹配测试的字符串</param>
/// <returns>域名</returns>
public static string GetURLDomain(string input)
{
return Regex.Match(input, @"^(?<Domain>(http://)?[^/]+/?)", RegexOptions.IgnoreCase).Groups["Domain"].Value;
}
var re = /^(http:\/\/)?[^\/]*\//g;
var rv ;
rv = "http://localhost/Store.WebSiteUI/Suggest.aspx".match(re);
document.write(rv+"<br />");
rv = "www.test.com/testweb/default.aspx".match(re);
document.write(rv);
</script>:)