Skip to content

Use regular expressions to query trigram indexes from JS clients

License

Notifications You must be signed in to change notification settings

bfulton/regex-trigram-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status NPM version

regex-trigram-js

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.

Usage

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": []
}

About

Use regular expressions to query trigram indexes from JS clients

Resources

License

Stars

Watchers

Forks

Packages

No packages published