Match strings in order





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















I have made a regex, but I can't make it to match in order.






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>





Here is what I expect to obtain as result in the string 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 |
|-----------------------------------------|









share|improve this question























  • It's the orginal string

    – executable
    Jan 3 at 11:27











  • stackoverflow.com/a/53945019/2813224

    – zer00ne
    Jan 3 at 11:31











  • This does not meet my requirements

    – executable
    Jan 3 at 11:34













  • Why would you expect centos:7 to be true?

    – zer00ne
    Jan 3 at 11:38











  • Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

    – barbsan
    Jan 3 at 11:38


















-3















I have made a regex, but I can't make it to match in order.






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>





Here is what I expect to obtain as result in the string 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 |
|-----------------------------------------|









share|improve this question























  • It's the orginal string

    – executable
    Jan 3 at 11:27











  • stackoverflow.com/a/53945019/2813224

    – zer00ne
    Jan 3 at 11:31











  • This does not meet my requirements

    – executable
    Jan 3 at 11:34













  • Why would you expect centos:7 to be true?

    – zer00ne
    Jan 3 at 11:38











  • Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

    – barbsan
    Jan 3 at 11:38














-3












-3








-3








I have made a regex, but I can't make it to match in order.






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>





Here is what I expect to obtain as result in the string 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 |
|-----------------------------------------|









share|improve this question














I have made a regex, but I can't make it to match in order.






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>





Here is what I expect to obtain as result in the string 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 |
|-----------------------------------------|





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>





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>






javascript jquery regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 11:21









executableexecutable

1,9832924




1,9832924













  • It's the orginal string

    – executable
    Jan 3 at 11:27











  • stackoverflow.com/a/53945019/2813224

    – zer00ne
    Jan 3 at 11:31











  • This does not meet my requirements

    – executable
    Jan 3 at 11:34













  • Why would you expect centos:7 to be true?

    – zer00ne
    Jan 3 at 11:38











  • Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

    – barbsan
    Jan 3 at 11:38



















  • It's the orginal string

    – executable
    Jan 3 at 11:27











  • stackoverflow.com/a/53945019/2813224

    – zer00ne
    Jan 3 at 11:31











  • This does not meet my requirements

    – executable
    Jan 3 at 11:34













  • Why would you expect centos:7 to be true?

    – zer00ne
    Jan 3 at 11:38











  • Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

    – barbsan
    Jan 3 at 11:38

















It's the orginal string

– executable
Jan 3 at 11:27





It's the orginal string

– executable
Jan 3 at 11:27













stackoverflow.com/a/53945019/2813224

– zer00ne
Jan 3 at 11:31





stackoverflow.com/a/53945019/2813224

– zer00ne
Jan 3 at 11:31













This does not meet my requirements

– executable
Jan 3 at 11:34







This does not meet my requirements

– executable
Jan 3 at 11:34















Why would you expect centos:7 to be true?

– zer00ne
Jan 3 at 11:38





Why would you expect centos:7 to be true?

– zer00ne
Jan 3 at 11:38













Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

– barbsan
Jan 3 at 11:38





Did you see the regex you're creating? I don't get why centos:7 is expected to be found in that string, propably you should set some requirements, and create proper regexes

– barbsan
Jan 3 at 11:38












2 Answers
2






active

oldest

votes


















1














You're creating regexes from strings like ^(?=.*?CentOS)(?=.*?7)(?=.*?live) which searches for given words. It lacks some restrictions, like:




  • this word can be followed only by - (or some other separator) or end of string

  • this word should be at beginning of the string or after - (or some other separator)


So, you need to create lookahead like:



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


instead of:



(?=.*?someword)


(For - as separator it would be: (?=^(.*[-])?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>








share|improve this answer


























  • What's the logic behind: Centos 7 livegnome : true"?

    – zer00ne
    Jan 3 at 12:01











  • @zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

    – barbsan
    Jan 3 at 12:04











  • Little issue, if I search like entOS-7 it will be true

    – executable
    Jan 3 at 12:05






  • 1





    @executable seems fine

    – barbsan
    Jan 3 at 12:55






  • 1





    @executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

    – barbsan
    Jan 3 at 13:31



















1














This is a solution, it might not be perfect but you should precise more your requirements it it fails in some cases. The regex will test if the words are in order and separated by the separators (if not at beginning or end). It will match entire words (x86 will be detected but not x8). There can be words between those specified. Some explanations:




  • positive lookaheads don't consume characters, so i don't think you can combine them to guarantee an order, il will only guarantee that they will be present afterwards

  • you should use double escaping \ if you build a RegExp from a string

  • no need to use filter to do another replace (it was in the duplicate post) -> EDIT: restored because it has another function: removing empty strings

  • you can use ?: to make a group for an alternative that doesn't capture, or if you want to quantify a group

  • if you have other questions about the regex, just ask..





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(/(rn|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>








share|improve this answer


























  • Thank you a lot for your answer ! I'll make some testing

    – executable
    Jan 4 at 15:41













  • I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

    – executable
    Jan 4 at 15:58













  • You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

    – Kaddath
    Jan 4 at 16:15













  • I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

    – executable
    Jan 7 at 8:09












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021327%2fmatch-strings-in-order%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You're creating regexes from strings like ^(?=.*?CentOS)(?=.*?7)(?=.*?live) which searches for given words. It lacks some restrictions, like:




  • this word can be followed only by - (or some other separator) or end of string

  • this word should be at beginning of the string or after - (or some other separator)


So, you need to create lookahead like:



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


instead of:



(?=.*?someword)


(For - as separator it would be: (?=^(.*[-])?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>








share|improve this answer


























  • What's the logic behind: Centos 7 livegnome : true"?

    – zer00ne
    Jan 3 at 12:01











  • @zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

    – barbsan
    Jan 3 at 12:04











  • Little issue, if I search like entOS-7 it will be true

    – executable
    Jan 3 at 12:05






  • 1





    @executable seems fine

    – barbsan
    Jan 3 at 12:55






  • 1





    @executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

    – barbsan
    Jan 3 at 13:31
















1














You're creating regexes from strings like ^(?=.*?CentOS)(?=.*?7)(?=.*?live) which searches for given words. It lacks some restrictions, like:




  • this word can be followed only by - (or some other separator) or end of string

  • this word should be at beginning of the string or after - (or some other separator)


So, you need to create lookahead like:



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


instead of:



(?=.*?someword)


(For - as separator it would be: (?=^(.*[-])?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>








share|improve this answer


























  • What's the logic behind: Centos 7 livegnome : true"?

    – zer00ne
    Jan 3 at 12:01











  • @zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

    – barbsan
    Jan 3 at 12:04











  • Little issue, if I search like entOS-7 it will be true

    – executable
    Jan 3 at 12:05






  • 1





    @executable seems fine

    – barbsan
    Jan 3 at 12:55






  • 1





    @executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

    – barbsan
    Jan 3 at 13:31














1












1








1







You're creating regexes from strings like ^(?=.*?CentOS)(?=.*?7)(?=.*?live) which searches for given words. It lacks some restrictions, like:




  • this word can be followed only by - (or some other separator) or end of string

  • this word should be at beginning of the string or after - (or some other separator)


So, you need to create lookahead like:



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


instead of:



(?=.*?someword)


(For - as separator it would be: (?=^(.*[-])?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>








share|improve this answer















You're creating regexes from strings like ^(?=.*?CentOS)(?=.*?7)(?=.*?live) which searches for given words. It lacks some restrictions, like:




  • this word can be followed only by - (or some other separator) or end of string

  • this word should be at beginning of the string or after - (or some other separator)


So, you need to create lookahead like:



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


instead of:



(?=.*?someword)


(For - as separator it would be: (?=^(.*[-])?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>








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>





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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 13:18

























answered Jan 3 at 12:00









barbsanbarbsan

2,65471524




2,65471524













  • What's the logic behind: Centos 7 livegnome : true"?

    – zer00ne
    Jan 3 at 12:01











  • @zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

    – barbsan
    Jan 3 at 12:04











  • Little issue, if I search like entOS-7 it will be true

    – executable
    Jan 3 at 12:05






  • 1





    @executable seems fine

    – barbsan
    Jan 3 at 12:55






  • 1





    @executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

    – barbsan
    Jan 3 at 13:31



















  • What's the logic behind: Centos 7 livegnome : true"?

    – zer00ne
    Jan 3 at 12:01











  • @zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

    – barbsan
    Jan 3 at 12:04











  • Little issue, if I search like entOS-7 it will be true

    – executable
    Jan 3 at 12:05






  • 1





    @executable seems fine

    – barbsan
    Jan 3 at 12:55






  • 1





    @executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

    – barbsan
    Jan 3 at 13:31

















What's the logic behind: Centos 7 livegnome : true"?

– zer00ne
Jan 3 at 12:01





What's the logic behind: Centos 7 livegnome : true"?

– zer00ne
Jan 3 at 12:01













@zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

– barbsan
Jan 3 at 12:04





@zer00ne string "CentOS-7-x86_64-LiveGNOME-1804" contains words "Centos", "7" and "livegnome" (it's case insensitive)

– barbsan
Jan 3 at 12:04













Little issue, if I search like entOS-7 it will be true

– executable
Jan 3 at 12:05





Little issue, if I search like entOS-7 it will be true

– executable
Jan 3 at 12:05




1




1





@executable seems fine

– barbsan
Jan 3 at 12:55





@executable seems fine

– barbsan
Jan 3 at 12:55




1




1





@executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

– barbsan
Jan 3 at 13:31





@executable it's really worth to create string first, and then pass it to RegExp so that you could debug it and see what it contains

– barbsan
Jan 3 at 13:31













1














This is a solution, it might not be perfect but you should precise more your requirements it it fails in some cases. The regex will test if the words are in order and separated by the separators (if not at beginning or end). It will match entire words (x86 will be detected but not x8). There can be words between those specified. Some explanations:




  • positive lookaheads don't consume characters, so i don't think you can combine them to guarantee an order, il will only guarantee that they will be present afterwards

  • you should use double escaping \ if you build a RegExp from a string

  • no need to use filter to do another replace (it was in the duplicate post) -> EDIT: restored because it has another function: removing empty strings

  • you can use ?: to make a group for an alternative that doesn't capture, or if you want to quantify a group

  • if you have other questions about the regex, just ask..





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(/(rn|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>








share|improve this answer


























  • Thank you a lot for your answer ! I'll make some testing

    – executable
    Jan 4 at 15:41













  • I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

    – executable
    Jan 4 at 15:58













  • You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

    – Kaddath
    Jan 4 at 16:15













  • I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

    – executable
    Jan 7 at 8:09
















1














This is a solution, it might not be perfect but you should precise more your requirements it it fails in some cases. The regex will test if the words are in order and separated by the separators (if not at beginning or end). It will match entire words (x86 will be detected but not x8). There can be words between those specified. Some explanations:




  • positive lookaheads don't consume characters, so i don't think you can combine them to guarantee an order, il will only guarantee that they will be present afterwards

  • you should use double escaping \ if you build a RegExp from a string

  • no need to use filter to do another replace (it was in the duplicate post) -> EDIT: restored because it has another function: removing empty strings

  • you can use ?: to make a group for an alternative that doesn't capture, or if you want to quantify a group

  • if you have other questions about the regex, just ask..





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(/(rn|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>








share|improve this answer


























  • Thank you a lot for your answer ! I'll make some testing

    – executable
    Jan 4 at 15:41













  • I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

    – executable
    Jan 4 at 15:58













  • You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

    – Kaddath
    Jan 4 at 16:15













  • I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

    – executable
    Jan 7 at 8:09














1












1








1







This is a solution, it might not be perfect but you should precise more your requirements it it fails in some cases. The regex will test if the words are in order and separated by the separators (if not at beginning or end). It will match entire words (x86 will be detected but not x8). There can be words between those specified. Some explanations:




  • positive lookaheads don't consume characters, so i don't think you can combine them to guarantee an order, il will only guarantee that they will be present afterwards

  • you should use double escaping \ if you build a RegExp from a string

  • no need to use filter to do another replace (it was in the duplicate post) -> EDIT: restored because it has another function: removing empty strings

  • you can use ?: to make a group for an alternative that doesn't capture, or if you want to quantify a group

  • if you have other questions about the regex, just ask..





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(/(rn|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>








share|improve this answer















This is a solution, it might not be perfect but you should precise more your requirements it it fails in some cases. The regex will test if the words are in order and separated by the separators (if not at beginning or end). It will match entire words (x86 will be detected but not x8). There can be words between those specified. Some explanations:




  • positive lookaheads don't consume characters, so i don't think you can combine them to guarantee an order, il will only guarantee that they will be present afterwards

  • you should use double escaping \ if you build a RegExp from a string

  • no need to use filter to do another replace (it was in the duplicate post) -> EDIT: restored because it has another function: removing empty strings

  • you can use ?: to make a group for an alternative that doesn't capture, or if you want to quantify a group

  • if you have other questions about the regex, just ask..





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(/(rn|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>








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(/(rn|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>





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(/(rn|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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 16:12

























answered Jan 4 at 15:31









KaddathKaddath

3,2621520




3,2621520













  • Thank you a lot for your answer ! I'll make some testing

    – executable
    Jan 4 at 15:41













  • I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

    – executable
    Jan 4 at 15:58













  • You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

    – Kaddath
    Jan 4 at 16:15













  • I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

    – executable
    Jan 7 at 8:09



















  • Thank you a lot for your answer ! I'll make some testing

    – executable
    Jan 4 at 15:41













  • I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

    – executable
    Jan 4 at 15:58













  • You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

    – Kaddath
    Jan 4 at 16:15













  • I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

    – executable
    Jan 7 at 8:09

















Thank you a lot for your answer ! I'll make some testing

– executable
Jan 4 at 15:41







Thank you a lot for your answer ! I'll make some testing

– executable
Jan 4 at 15:41















I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

– executable
Jan 4 at 15:58







I found an issue, if my search value is like *centos*. I think it's because we don't remove the empty string and the filter function corrected that issue

– executable
Jan 4 at 15:58















You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

– Kaddath
Jan 4 at 16:15







You're right, i restored the filter. I find strange that it should be a positive result because it has * at the beginning while the file has nothing before CentOS, but that would require changes to take that in account

– Kaddath
Jan 4 at 16:15















I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

– executable
Jan 7 at 8:09





I think it's because it didn't escape so it break the regular expression. So we can't detect in the order of the filename ?

– executable
Jan 7 at 8:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021327%2fmatch-strings-in-order%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]