🚧 JavaScript Regular Expression Visualizer.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 398d0b6 Aug 7, 2018
Permalink
Failed to load latest commit information.
src Fixed: chrome forbade open data uri Oct 30, 2017
tests Fixed: #33 Aug 24, 2016
.gitignore . Aug 24, 2016
LICENSE Publish Nov 13, 2014
README.md Additional fixes for README Aug 4, 2018
build-config.js Fixed:#9 build script Nov 30, 2014
index.html Fixed: chrome forbade open data uri Oct 30, 2017
index.js Fixed:npm main Sep 15, 2015
package.json Fixed:npm main Sep 15, 2015

README.md

Regulex

Regulex is a JavaScript Regular Expression Parser & Visualizer.

Features

  • Written in pure JavaScript. No backend needed.
  • You can embed the graph in you own site through HTML iframe element.
  • Detailed error message. In most cases it can point out the precise syntax error position.
  • No support for octal escape. Yes it is a feature. ECMAScript strict mode doesn't support octal escape in string, but many browsers still support octal escape in regex. I make things easier. In regulex, DecimalEscape will always be treated as back reference. If the back reference is invalid, e.g. /\1/, /(\1)/, /(a)\2/, or DecimalEscape appears in charset(because in this case it can't be explained as back reference, e.g. /(ab)[\1]/, Regulex will always throw an error.

Build

This command will generate dest/regulex.js.

npm install -g requirejs
r.js -o build-config.js

API

Parse to AST

var parse = require("regulex").parse;
var re = /var\s+([a-zA-Z_]\w*);/ ;
console.log(parse(re.source));

Visualize

var parse = require("regulex").parse;
var visualize = require("regulex").visualize;
var Raphael = require('regulex').Raphael;
var re = /var\s+([a-zA-Z_]\w*);/;
var paper = Raphael("yourSvgContainer", 0, 0);
try {
  visualize(parse(re.source), getRegexFlags(re), paper);
} catch(e) {
  if (e instanceof parse.RegexSyntaxError) {
    logError(re, e);
  } else {
    throw e;
  }
}

function logError(re, err) {
  var msg = ["Error:" + err.message, ""];
  if (typeof err.lastIndex === "number") {
    msg.push(re);
    msg.push(new Array(err.lastIndex).join("-") + "^");
  }
  console.log(msg.join("\n"));
}


function getRegexFlags(re) {
  var flags = "";
  flags += re.ignoreCase ? "i" : "";
  flags += re.global ? "g" : "";
  flags += re.multiline ? "m" : "";
  return flags;
}