Skip to content

Instantly share code, notes, and snippets.

@horatio-sans-serif
Created April 2, 2010 20:34
Show Gist options
  • Save horatio-sans-serif/353672 to your computer and use it in GitHub Desktop.
Save horatio-sans-serif/353672 to your computer and use it in GitHub Desktop.
var qmarkRE = /\?/g;
var starRE = /\*/g;
var dotRE = /\./g;
sys=require('sys')
function fnmatch (pattern, test) {
var newPattern = pattern.replace(dotRE, '(\\.)')
.replace(qmarkRE, '(.)')
.replace(starRE, '(.*?)');
return (new RegExp(newPattern)).test(test);
}
var assert = require("assert");
assert.equal(fnmatch("h*llo","hello"), true);
assert.equal(fnmatch("h[ae]llo","hallo"), true);
assert.equal(fnmatch("h[ae]llo","hello"), true);
assert.equal(fnmatch("h[ae]llo","hillo"), false);
assert.equal(fnmatch("h?llo","hillo"), true);
assert.equal(fnmatch("h?llo","hllo"), false);
assert.equal(fnmatch("h?llo","h\u0043llo"), true);
assert.equal(fnmatch("h?llo","h\u0043llo"), true);
assert.equal(fnmatch("h*llo*","hullo wurld"), true);
assert.equal(fnmatch("*.*.*.*","192.168.0.1"), true);
assert.equal(fnmatch("*.*.?.?","192.168.0.1"), true);
assert.equal(fnmatch("?[92][92].*.?.?","128.168.0.1"), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment