正在表达式匹配域名

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>匹配域名</title>
  <style type="text/css">
  </style>
</head>

<body>
  <script type="text/javascript">
  var str = "https://www.baidu.com/s?cl=3";
  var reg = /http(s)?:\/\/([A-Za-z0123456789:\.]+)\/\S+/;
  var result = reg.exec(str);
  if (result) {
    console.log(result[2]);  //www.baidu.com
  }


  var str = "http://10.10.10.66:3000/s?cl=3";
  var reg = /http(s)?:\/\/([A-Za-z0123456789:\.]+)\/\S+/;
  var result = reg.exec(str);
  if (result) {
    console.log(result[2]);  //10.10.10.66:3000
  }  
  </script>
</body>

</html>

 

posted @ 2018-01-08 13:17  徐同保  阅读(7)  评论(0编辑  收藏  举报  来源