0

I want to just support asterisk(*) wildcard not regex.

This is my code :

for example : checkNames[i] = "Number of ongoing MS sessions"


var checkName = checkNames[i].split("*").join(".*");
supportRegExp(dataSource[j].ColumnName, checkName, validatedList,dataSource[j]);

and this is my supportRegExp function :

function supportRegExp(arrayElem, checkName, validatedList, elem) {
    var regexpArr = checkName
        .replace(/\t/g, '\n')
        .split("\n")
        .map(function (item) { 
            return "^" + item + "$"; 
        });
    var regexp = new RegExp(regexpArr, 'i');
    $my.isMatched = regexp.test(arrayElem)
    if ($my.isMatched) {
        validatedList.push(elem);  //elem: my object { inside ColumnName, DisplayName }
        AddValidatedList(arrayElem); //arrayElem : elem.ColumnName or elem.DisplayName bla bla.
    }
}

This is works. I am writing "num*" and coming result and then I am writing "num|m" and coming result. Because I am using regexp so I want to just support '*' sign.

For example : When I am writing num*, result should come but I am writing num|m should not come result. Because I want to just support asterisk sign.

How can I do ?

Any idea please.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
eagle
  • 451
  • 1
  • 7
  • 27
  • Do you mean you want to be able to specify a literal `*` symbol in the pattern? You need to escape it in some way. Regexes use ``\``, Lua uses `%`, MS Office uses `~`. Use any :) – Wiktor Stribiżew Dec 23 '16 at 09:11
  • 1
    @WiktorStribiżew It looks like he wants the `*` to be converted to `.*` so it is handled as a glob, but wants all other regular expression metacharacters to be ignored. – Phylogenesis Dec 23 '16 at 09:13
  • @Phylogenesis: Could you please edit the question? It is rather hard to understand right now – Wiktor Stribiżew Dec 23 '16 at 09:16
  • Why are you passing an array of regex expressions to `new RegExp()`? Did you mean to do `new RegExp(regexpArr.join("|"), 'i');`? That doesn't look correct to me. Also, you will need to escape ALL other characters, other than `*`. See here for help: http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – James Wilkins Jan 12 '17 at 20:08

1 Answers1

1

Borrowing heavily from this question, I think you are after something like the following:

var checkName =
    checkNames[i]
        .split("*")
        .map(function (s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); })
        .join(".*");

This escapes each part of the regular expression after splitting on the * character, ready to be joined back with .*.

Community
  • 1
  • 1
Phylogenesis
  • 7,775
  • 19
  • 27