如何从正则表达式中显示url提供程序名称

时间:2017-06-20 07:53:34

标签: angularjs regex

我有这样的要求,

http://indianexpress.com/article/india/ram-nath-kovind-for-president-which-parties-support-his-candidature-and-how-the-scales-weigh-4712714/

现在显示整个链接,现在要求是,只显示indianexpress(当我点击此链接时应该导航)

代码是:

     var detectUrls = function(value) {
          var retval = value;
          if(retval != null) {
            var urlRegex = /(((\b(https?|ftp|file):\/\/)|(www\.))[-A-Z0-9+&@#\/%?=~_|!:(),.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
            retval = value.replace(urlRegex, function (url,b,c) {
              var url2 = (c == 'www.') ?  'http://' +url : url;
              return '<a href="' + url2 + '" target="_blank">' + url2 + '</a>';
            });
          }
          return retval;
        };
$scope.post.postText = detectUrls($scope.post.postText);

我使用url2(上图)显示整个链接

1 个答案:

答案 0 :(得分:1)

我建议重新组合正则表达式的各个部分,只有2个相关的捕获组,一个用于协议,另一个用于具有任何子域的域名。 模式是

  • ((?:\b(?:https?|ftp|file):\/\/)|www\.) - 第1组(b):
    • (?:\b(?:https?|ftp|file):\/\/) - 单词boundar后跟httphttpsftpfile,后面加://
    • | - 或
    • www\. - www. substring
  • ([^\s\/]+) - 第2组(c):除了空格以外的1 +个字符/
  • (?:\/?[-A-Z0-9+&@#\/%?=~_|!:(),.;]*[-A-Z0-9+&@#\/%=~_|])? - 可选序列:
  • \/? - 1或0 / s
  • [-A-Z0-9+&@#\/%?=~_|!:(),.;]* - 字符类
  • 中定义的0个或更多字符
  • [-A-Z0-9+&@#\/%=~_|] - 定义的类中的1个单个字符。

然后,代码看起来像

var detectUrls = function(value) {
      var retval = value;
      if(retval != null) {
        var urlRegex = /((?:\b(?:https?|ftp|file):\/\/)|www\.)([^\s\/]+)(?:\/?[-A-Z0-9+&@#\/%?=~_|!:(),.;]*[-A-Z0-9+&@#\/%=~_|])?/ig;
        retval = value.replace(urlRegex, function (url,b,c) {
          var url2 = (b == 'www.') ?  'http://' +url : url;
          return '<a href="' + url2 + '" target="_blank">' + c + '</a>';
        });
      }
      return retval;
    };
    
var res  = detectUrls('http://indianexpress.com/article/india/ram-nath-kovind-for-president-which-parties-support-his-candidature-and-how-the-scales-weigh-4712714/  www.google.com');
console.log(res);
document.body.innerHTML = res;

要仅显示需要稍微调整正则表达式的名称,([^\s\/]+)将更改为(?:[^\s\/]*\.)?([^\s\/.]+)\.[^\s\/.]+

  • (?:[^\s\/]*\.)? - 除了空格之外的0+字符的可选序列, /后跟.
  • ([^\s\/.]+) - 第2组(c)将匹配除空格以外的1个字符/.
  • \. - .
  • [^\s\/.]+ - 除了空格以外的1个字符/.

var detectUrls = function(value) {
      var retval = value;
      if(retval != null) {
        var urlRegex = /((?:\b(?:https?|ftp|file):\/\/)|www\.)(?:[^\s\/]*\.)?([^\s\/.]+)\.[^\s\/.]+(?:\/?[-A-Z0-9+&@#\/%?=~_|!:(),.;]*[-A-Z0-9+&@#\/%=~_|])?/ig;
        retval = value.replace(urlRegex, function (url,b,c) {
          var url2 = (b == 'www.') ?  'http://' +url : url;
          return '<a href="' + url2 + '" target="_blank">' + c + '</a>';
        });
      }
      return retval;
    };
    
var res  = detectUrls('http://indianexpress.com/article/india/ram-nath-kovind-for-president-which-parties-support-his-candidature-and-how-the-scales-weigh-4712714/  www.google.com');
console.log(res);
document.body.innerHTML = res;