This project ports some ideas Russ Cox shared on
how Google Code Search works — in
particular the RegexpQuery and related functions —
from Go to JavaScript. Since JavaScript doesn't have an equivalent to Go's regexp/syntax,
use PEG.js to introduce a simplified regular
expression grammar and obtain a parse tree.
The goal is to query trigram indexes from JS clients.
Include the library.
var regex = require('regex-trigram');Parse a regular expression.
var re = regex.parse("a[bc]d");
console.log(JSON.stringify(re, null, 2));Which should look like:
{
  "type": "concat",
  "value": [
    {
      "type": "literal",
      "value": "a"
    },
    {
      "type": "concat",
      "value": [
        {
          "type": "char_class",
          "value": "bc"
        },
        {
          "type": "literal",
          "value": "d"
        }
      ]
    }
  ]
}Convert the parsed regular expression into a trigram query.
var q = regex.query(re);
console.log(JSON.stringify(q, null, 2));Which should look like:
{
  "op": "OR",
  "trigram": [
    "abd",
    "acd"
  ],
  "sub": []
}