按顺序匹配字符串

时间:2019-01-03 11:21:56

标签: javascript jquery regex

我制作了一个正则表达式,但是无法使其顺序匹配。

var $result = [];
var url_check = "CentOS-7-x86_64-LiveGNOME-1804";
var torrent_forbidden = ["CentOS-7 live", "Centos 7 livegnome", "Cent-7", "OS Cent-7", "centos:7", "centos word:7", "centos:6", "cento 7 s"];
jQuery.each(torrent_forbidden , function(index, torrent_forbidden) { 
    var regex = new RegExp('^(?=.*?' + torrent_forbidden.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').split(/\\?[\s,_.:*-]+/).join(')(?=.*?') + ')', 'gi');
    if(regex.test(url_check) === true){
        $result.push(torrent_forbidden + ' : true');
    }else{
        $result.push(torrent_forbidden + ' : false');
    }
});
console.log($result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

这是我期望通过字符串CentOS-7-x86_64-LiveGNOME-1804获得的结果:

|-----------------------------------------|
| Search             | Result | Expected  |
|-----------------------------------------|
| CentOS-7 live      | true   | false     |
| Centos 7 livegnome | true   | true      |
| Cent-7             | true   | false     |
| OS Cent-7          | true   | false     |
| centos:7           | true   | true      |
| centos word:7      | false  | false     |
| centos:6           | true   | false     |
| cento 7 s          | true   | false     |
|-----------------------------------------|

2 个答案:

答案 0 :(得分:1)

您正在使用^(?=.*?CentOS)(?=.*?7)(?=.*?live)之类的字符串创建正则表达式,该正则表达式搜索给定的单词。它没有一些限制,例如:

  • 此单词只能跟-(或其他分隔符)或字符串结尾
  • 该单词应位于字符串开头或-(或其他分隔符)之后

因此,您需要像这样创建前瞻:

(?=^(.*separators)?someword(separators|$))  

而不是:

(?=.*?someword)

(对于-作为分隔符,应为:(?=^(.*[-])?someword([-]|$))

var $result = [];
var url_check = "CentOS-7-x86_64-LiveGNOME-1804";
var torrent_forbidden = ["CentOS-7 live", "Centos 7 livegnome", "Cent-7", "OS Cent-7", "centos:7", "centos word:7", "centos:6", "cento 7 s", "entOS-7", "*centos*"];
jQuery.each(torrent_forbidden , function(index, torrent_forbidden) { 
   var regexstr = '^(?=^(.*[-])?' + torrent_forbidden.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').split(/\\?[\s,_.:*-]+/).join('([-]|$))(?=^(.*[-])?') + '([-]|$))';
   console.log(regexstr)
    var regex = new RegExp(regexstr, 'gi');
    if(regex.test(url_check) === true){
        $result.push(torrent_forbidden + ' : true');
    }else{
        $result.push(torrent_forbidden + ' : false');
    }
});
console.log($result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

答案 1 :(得分:1)

这是一个解决方案,它可能并不完美,但是您应该更加精确地满足您的要求,在某些情况下它会失败。正则表达式将测试单词是否有序并由分隔符分隔(如果不是在开头或结尾)。它将匹配整个单词(将检测到x86,但不会检测到x8)。在指定的单词之间可以有单词。一些解释:

  • 积极的前瞻性不消耗字符,因此我认为您不能将它们组合起来以保证订单,il只会保证它们随后会出现
  • 如果从字符串构建RegExp,则应使用双转义\\
  • 无需使用filter做另一个replace(在重复的帖子中)->编辑:已恢复,因为它具有另一个功能:删除空字符串
  • 您可以使用?:来为无法捕获的替代物或要量化的组建立一个组
  • 如果您对正则表达式还有其他疑问,请问..

var $result = [];
var url_check = "CentOS-7-x86_64-LiveGNOME-1804";
var torrent_forbidden = ["CentOS-7 live", "Centos 7 livegnome", "Cent-7", "OS Cent-7", "centos:7", "centos word:7", "centos:6", "cento 7 s", "CentOS x86", "CentOS x8", "*CentOS*"];
jQuery.each(torrent_forbidden , function(index, torrent_forbidden) { 
    var regexstr = '(?:^|[\\s,_.:*-])' + torrent_forbidden
      .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
      .split(/\\?[\s,_.:*-]+/)
      .filter( function(e){ return e.replace(/(\r\n|\n|\r)/gm,""); } )
      .join('(?:(?:[\\s,_.:*-][^\\s,_.:*-]+)+)?[\\s,_.:*-]') + '(?:[\\s,_.:*-]|$)';
    console.log(regexstr); //To debug your regexes
    var regex = new RegExp(regexstr, 'gi');
    if(regex.test(url_check) === true){
        $result.push(torrent_forbidden + ' : true');
    }else{
        $result.push(torrent_forbidden + ' : false');
    }
});
console.log($result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

相关问题