【React Native】正则判断字符串中是否包含url、提取字符串中的url

  1、字符串中是否包含某个指定的url

checkInviteUrl(text) {
        var isLega = true
        if (text.indexOf("https://meteora.xyz/#/t/") !== -1) {

            let str = text.slice(-10)
            for(var idx =0;idx < str.length;idx++) {
                var asc = str.charCodeAt(idx)
                if (!(asc >= 48 && asc <= 57 || asc >= 65 && asc <= 90 || asc >= 97 && asc <= 122)) {
                    isLega = false;
                }
            }
        }  else {
            isLega = false
        }
        return isLega
    }

  2、字符串中是否包含url

checkUrlWithString(str_url){
        var strRegex = "((https|http|ftp|rtsp|mms)?://)"
            + "(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
            + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
            + "|" // 允许IP和DOMAIN(域名)
            + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.
            + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
            + "[a-z]{2,6})" // first level domain- .com or .museum
            + "(:[0-9]{1,4})?" // 端口- :80
            + "((/?)|" // a slash isn't required if there is no file name
            + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)";
        var re = new RegExp(strRegex);
        if (re.test(str_url)){
            return true
        } else {
            return false
        }
    }

  3、提取字符串中的url

getUrlWithString(s) {
        // var reg = "(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)";
        var reg = "(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        s = s.match(reg);
        return(s)
    }

 

posted @ 2020-04-22 11:05  Mr·Xu  阅读(2117)  评论(2编辑  收藏  举报