zoukankan      html  css  js  c++  java
  • 【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)
        }
  • 相关阅读:
    【转载】大型系统中使用JMS优化技巧
    【原创】JMS发布者订阅者【异步接收消息】
    【原创】JMS生产者和消费者【PTP异步接收消息】
    泛型
    For-Each循环
    策略模式(Strategy)
    Sort--快速排序
    Serach
    Sort--冒泡排序
    数值交换
  • 原文地址:https://www.cnblogs.com/xjf125/p/12750655.html
Copyright © 2011-2022 走看看