0

I'm trying to match some words in a string. But I don't have a predefined number of words I need to find.

For example I search for Ubuntu 18 10 in ubuntu-18.10-desktop-amd64.iso.torrent would return true.

Or I could search for centos 7 in CentOS-7-x86_64-LiveGNOME-1804.torrent would also return true.

I don't need to check if it's lowercase or not.

What I tried :

$.get('interdit', function(data) {
    var lines = data.split("\n");
    $.each(lines, function(n, data_interdit) {
        var url_check = $('textarea#url').val()
        var split_forbidden = data_interdit.split(/[\s|,|_|.|-|:]+/);
        var exist = 0;
        $.each(split_forbidden, function(n, data) {
            var n = url_check.search("^("+ data +")");
            if(n != -1){
                exist = 1
            }else{
                exist = 0
            }
            console.log('Forbidden: '+ data + ' Result: ' + n);
        })
        if(exist == 1){
            console.log('found')
        }
    });
});

Sample data of the file interdit :

CentOS.7
Ubuntu-18
executable
  • 3,365
  • 6
  • 24
  • 52

2 Answers2

1

You want to look for existing words within the input string without the order being taken into account. You need to use positive lookaheads for this:

var search = 'Ubuntu 18 10';
var str = 'ubuntu-18.10-desktop-amd64.iso.torrent';
var re = new RegExp('^(?=.*' + search.split(/[\s,_.:-]+/).join(')(?=.*') + ')', 'i')

console.log(re.test(str));

This produces a regex as the following (with i flag set):

^(?=.*Ubuntu)(?=.*18)(?=.*10)
revo
  • 47,783
  • 14
  • 74
  • 117
  • I'm getting the error `Invalid regular expression: /^(?=.**centos*)/: Nothing to repeat`. If in my file I have the following `*centos*` – executable Dec 28 '18 at 11:44
  • You should escape all special characters before passing it to constructor. [See here](https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex). – revo Dec 28 '18 at 12:00
  • Well I have a new error https://i.imgur.com/HgSTrXu.png and my search string contain `*ubuntu*` in this case – executable Dec 28 '18 at 12:30
  • Please provide a live demo. I can't find from where `\)` is added in your regex. It should be `)` with no backslashes. – revo Dec 28 '18 at 13:17
  • I created a jsbin here https://jsbin.com/tezejicugi/edit?js,console,output – executable Dec 28 '18 at 13:18
  • Don't you think you should add `*` beside other characters in split method regex? otherwise what should regex look for? – revo Dec 28 '18 at 13:24
  • Well I added this part of code to remove the first and last character `search = search .substring(1, search .length-1);` – executable Dec 28 '18 at 13:30
  • Please check my answer at your other question. – revo Dec 28 '18 at 13:31
  • Can I ask how to make the order to be taken into account – executable Jan 03 '19 at 11:37
1

RegEx Array

Update

"The code give me an error jsbin.com/pecoleweyi/2/edit?js,console"

Although the question did not include unlikely input such as: *centos 7*, add the following line to escape the special characters that occur in input:

var esc = word.replace(/[.*+?^${}()|[\]\\]/gi, '\\$&');

and change the next line:

var sub = esc.replace(/\s/gi, '.');

The demo below will:

  • accept a string (str) to search and an array of strings (tgt) to find within the string,

  • .map() the array (tgt) which will run a function on each string (word)

  • escape any special characters:

    var esc = word.replace(/[.*+?^${}()|[\]\\]/gi, '\\$&');
    
  • replace any spaces (/\s/g) with a dot (.):

    var sub = esc.replace(/\s/g, '.');
    
  • then makes a RegExp() Object so a variable can be inserted in the pattern via template literal interpolation (say that ten times fast):

    var rgx = new RegExp(`${sub}`, `gim`);
    
  • uses .test() to get a boolean: found = true / not found = false

    var bool = rgx.test(str);
    
  • create an Object to assign the search string: word as a property and the boolean: bool as it's value.

    var obj = {
      [word]: bool
    };
    
  • returns an array of objects:

    [{"centos 7":true},{"Ubuntu 18 10":true}]
    

Demo

var str = `ubuntu-18.10-desktop-amd64.iso.torrent 
CentOS-7-x86_64-LiveGNOME-1804.torrent`;

var tgt = [`centos 7`, `Ubuntu 18 10`, `corn flakes`, `gnome`, `Red Hat`, `*centos 7*`];

function rgxArray(str, tgt) {
  var res = tgt.map(function(word) {
    var esc = word.replace(/[.*+?^${}()|[\]\\]/gi, '\\$&');
    var sub = esc.replace(/\s/gi, '.');
    var rgx = new RegExp(`${sub}`, `gi`);
    var bool = rgx.test(str);
    var obj = {
      [word]: bool
    };
    return obj;
  });
  return res;
}

console.log(JSON.stringify(rgxArray(str, tgt)));
zer00ne
  • 41,936
  • 6
  • 41
  • 68