正则如何提取网址的主域名的首字母?

    let arr = [
        'http://baidu.com',
        'https://baidu.com',
        'http://map.baidu.com',
        'https://map.baidu.com',
        'http://www.baidu.com',
        'https://www.baidu.com',
        'www.baidu.com',
        'baidu.com',
        'map.baidu.com',
        'pic.baidu.com',

        'http://baidu.com.cn',
        'https://baidu.com.cn',
        'http://map.baidu.com.cn',
        'https://map.baidu.com.cn',
        'http://www.baidu.com.cn',
        'https://www.baidu.com.cn',
        'www.baidu.com.cn',
        'baidu.com.cn',
        'map.baidu.com.cn',
        'pic.baidu.com.cn',


        'http://baidu.com/mm',
        'https://baidu.com/mm',
        'http://map.baidu.com/mm',
        'https://map.baidu.com/mm',
        'http://www.baidu.com/mm',
        'https://www.baidu.com/mm',
        'www.baidu.com/mm',
        'baidu.com/mm',
        'map.baidu.com/mm',
        'pic.baidu.com/mm',

        'http://baidu.com.cn/mm/c',
        'https://baidu.com.cn/mm/c',
        'http://map.baidu.com.cn/mm/c',
        'https://map.baidu.com.cn/mm/c',
        'http://www.baidu.com.cn/mm/c',
        'https://www.baidu.com.cn/mm/c',
        'www.baidu.com.cn/mm/c',
        'baidu.com.cn/mm/c',
        'map.baidu.com.cn/mm/c',
        'pic.baidu.com.cn/mm/c',   

        'http://www.baidu.co.jp',
        'http://www.baidu.net',
        'http://baidu.fun/mm'

    ]

能匹配外国网址
如日本  .jp   .en 等

如上,以baidu.com为例,都提取 b
facebook.com 为例,都提取f
可适用其他域名。

主旨:浏览器地址栏能打开的网址,就提取它的域名首字母。

在线正则工具 https://regex101.com/

阅读 3k
3 个回答
    let test_str = [
        'http://baidu.com',
        'https://baidu.com',
        'http://map.baidu.com',
        'https://map.baidu.com',
        'http://www.baidu.com',
        'https://www.baidu.com',
        'www.baidu.com',
        'baidu.com',
        'map.baidu.com',
        'pic.baidu.com',

        'http://baidu.com.cn',
        'https://baidu.com.cn',
        'http://map.baidu.com.cn',
        'https://map.baidu.com.cn',
        'http://www.baidu.com.cn',
        'https://www.baidu.com.cn',
        'www.baidu.com.cn',
        'baidu.com.cn',
        'map.baidu.com.cn',
        'pic.baidu.com.cn',


        'http://baidu.com/mm',
        'https://baidu.com/mm',
        'http://map.baidu.com/mm',
        'https://map.baidu.com/mm',
        'http://www.baidu.com/mm',
        'https://www.baidu.com/mm',
        'www.baidu.com/mm',
        'baidu.com/mm',
        'map.baidu.com/mm',
        'pic.baidu.com/mm',

        'http://baidu.com.cn/mm/c',
        'https://baidu.com.cn/mm/c',
        'http://map.baidu.com.cn/mm/c',
        'https://map.baidu.com.cn/mm/c',
        'http://www.baidu.com.cn/mm/c',
        'https://www.baidu.com.cn/mm/c',
        'www.baidu.com.cn/mm/c',
        'baidu.com.cn/mm/c',
        'map.baidu.com.cn/mm/c',
        'pic.baidu.com.cn/mm/c',   

        'http://www.baidu.co.jp',
        'http://www.baidu.net',
        'http://baidu.fun/mm'        
    ]

    let r1 = /([\w\-]+?\.[\w\-]+?\.[\w]{2}?$)/
    let r2 = /([\w\-]+?\.[\w\-]+?$)/
    let r3 = /([\w\-]+?\.[\w\-]+?\.[\w]{2}?\/)/
    let r4 = /([\w\-]+?\.[\w\-]+?\/)/
    for (let str of test_str) {
        if (str.match(r1)) {
            result = str.match(r1)
        } else if (str.match(r2)) {
            result = str.match(r2)
        } else if (str.match(r3)) {
            result = str.match(r3)
        } else if (str.match(r4)) {
            result = str.match(r4)
        } else {
            result = '-'
        }
        console.log(result[1][0])
    }

输出全是 b

这个其实估计不能一次用规则式处理,可以考虑分步处理

  1. 首先提取URI的host信息
  2. 对host进行处理,获得主域名
  3. 获取主域名的首字母

比如 brokkr 方法中,对于 数据

'http://baidu.fun/mm'

就是无效的。

点击查看

^(?:(?:https?:\/\/)?(?:www\.)?)?\b([a-zA-Z])

解法1:

/**入参为url数组**/
function getVal(arr){
    var regex = /^(?:(?:https?:\/\/)?(?:www\.)?)?\b([a-zA-Z])/;
    var result=[];
    var match=null;
    for (var i in arr) {
        match=test_str[i].match(regex);
        if(match){
            result.push(match[1]);
        }
    }
    return result;
}

解法2:

function getVal(arr){
    var str=arr.join('\n');
    var regex = /^(?:(?:https?:\/\/)?(?:www\.)?)?\b([a-zA-Z])/mg;
    var result=[];
    var match=null;
    while((match=regex.exec(str))!=null){
        result.push(match[1]);
    }
    return result;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题