0

I've got this function to match wildcard strings:

stringMatchesWildcardPattern = function(string, wildcardPattern) {
    var matches = null;

    // Make string "event1" match wildcard pattern "event1.*"
    if(wildcardPattern.endsWith('.*')) {
        wildcardPattern = wildcardPattern.replaceLast('.*', '*');
    }

    // Build the pattern
    var regularExpressionPattern = wildcardPattern.split('*').join('.*');
    regularExpressionPattern = "^" + regularExpressionPattern + "$"

    // Create the expression
    var regularExpression = new RegExp(regularExpressionPattern);

    // Evaluate the expression
    matches = regularExpression.test(string);

    return matches;
};

Unfortunately, it is not robust and fails with this input which should pass:

stringMatchesWildcardPattern('one.two.*', 'one.*.three.*');

I'm having a hard time wrapping my head around how to solve this one. Been looking at the problem too long. Any ideas on how I can update my function to address this input?

Here are my test cases:

Assert.true(stringMatchesWildcardPattern('event1', 'event1.*'), '"event1" matches wildcard pattern, "event1.*"');
Assert.true(stringMatchesWildcardPattern('event1.secondLevelEvent1', 'event1.*'), '"event1.secondLevelEvent1" matches wildcard pattern, "event1.*"');
Assert.true(stringMatchesWildcardPattern('event1.secondLevelEvent1.thirdLevelEvent1', 'event1.secondLevelEvent1.*'), '"event1.secondLevelEvent1.thirdLevelEvent1" matches wildcard pattern, "event1.secondLevelEvent1.*"');
Assert.true(stringMatchesWildcardPattern('event1.secondLevelEvent1.thirdLevelEvent1', 'event1.*'), '"event1.secondLevelEvent1.thirdLevelEvent1" matches wildcard pattern, "event1.*"');
Assert.true(stringMatchesWildcardPattern('event123', 'event*'), '"event123" matches wildcard pattern, "event*"');
Assert.true(stringMatchesWildcardPattern('event123event', 'event*event'), '"event123event" matches wildcard pattern, "event*event"');
Assert.true(stringMatchesWildcardPattern('123event123event123', '*event*event*'), '"123event123event123" matches wildcard pattern, "*event*event*"');
Assert.true(stringMatchesWildcardPattern('keyboard.key.*.up', 'keyboard.key.*.up.*'), '"keyboard.key.*.up" matches wildcard pattern, "keyboard.key.*.up.*"');
Assert.false(stringMatchesWildcardPattern('event', 'event1.*'), '"event" does not match wildcard pattern "event1.*"');
Assert.false(stringMatchesWildcardPattern('event', 'event1.*.*'), '"event" does not match wildcard pattern "event1.*.*"');
Assert.false(stringMatchesWildcardPattern('123event', 'event'), '"123event" does not match wildcard pattern, "event"');
Assert.false(stringMatchesWildcardPattern('123event123', 'event*'), '"123event123" does not match wildcard pattern, "event*"');
Assert.false(stringMatchesWildcardPattern('123event123event123', 'event*event*'), '"123event123event123" does not match wildcard pattern, "event*event*"');
Assert.true(stringMatchesWildcardPattern('one.two.*', 'one.*.three.*'), '"one.two.*" match wildcard pattern, "one.*.three.*"');
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
  • I would expect your failing input to *not* match, because the pattern you're matching against has the text `three` in the middle, but the test string does not. When you use `one.*.three.*`, is each `.` supposed to match a literal `.`, or is that a regex `.` that matches any character? – nnnnnn Jul 02 '16 at 00:39
  • Can you add a example of possible input and expected output. – Rajshekar Reddy Jul 02 '16 at 00:40
  • Regular expressions shouldn't be edited this way. Where you get the regex is a mystery. If you're getting it from user input as a lightweight search pattern you're letting the user directly pass a _regex_ to the engine. Very dangerous unless you take a lot of precautions. On the other hand, If you are letting the user do a single wildcard `*` to be converted to regex, you have to do a custom escape on the _other_ possible metachars first. Don't split ! Do a global replace `([+?()\[\]^$.{}])` with `\\$1` first. Then global replace `[*]+` with `.*?`. –  Jul 02 '16 at 01:47
  • @Reddy I updated my question with some test cases – Kirk Ouimet Jul 02 '16 at 04:17
  • Can you explain how `stringMatchesWildcardPattern` is supposed to behave? – asemahle Jul 02 '16 at 04:24
  • 1
    Found my answer at http://stackoverflow.com/questions/18695727/algorithm-to-find-out-whether-the-matches-for-two-glob-patterns-or-regular-expr – Kirk Ouimet Jul 02 '16 at 05:25
  • Utter baloney !! Such an _intersection_ is meaningless and says nothing about a target ... The equation can't be solved. –  Jul 02 '16 at 19:11

0 Answers0