Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created November 2, 2016 06:07
Show Gist options
  • Save nobeans/dae3f063459845b4b29b446e12a4bf74 to your computer and use it in GitHub Desktop.
Save nobeans/dae3f063459845b4b29b446e12a4bf74 to your computer and use it in GitHub Desktop.
function test(pattern, text, expected) {
const regexp = new RegExp(pattern)
const matched = regexp.test(text);
console.log(pattern, text, matched, (matched === expected) ? "OK" : "NG");
}
test("a", "aaa", true);
test(".a.", "aaa", true);
test("x", "aaa", false);
test("^/a/b/c", "/a/b/c/d/e", true);
test("^/a / b/c", "/a / b/c/d/e", true);
test("^/a$/b/c", "/a$/b/c/d/e", false); // $が正規表現パターンとして使われてしまう
test("^/a.x/b/c", "/a#x/b/c/d/e", true); // .が正規表現パターンとして使われてしまう
//=>
// a aaa true OK
// .a. aaa true OK
// x aaa false OK
// ^/a/b/c /a/b/c/d/e true OK
// ^/a / b/c /a / b/c/d/e true OK
// ^/a$/b/c /a$/b/c/d/e false OK
// ^/a.x/b/c /a#x/b/c/d/e true OK
//
// *** time: 0.197457 ***
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment