Skip to content

Instantly share code, notes, and snippets.

@KylePDavis
Last active October 10, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KylePDavis/3755133 to your computer and use it in GitHub Desktop.
Save KylePDavis/3755133 to your computer and use it in GitHub Desktop.
XRegExp tokenizer and parser that builds a new RegExp to validate strings against a given format string
// Get XRegExp in all it's goodness and install native stuff
var XRegExp = require("xregexp");
XRegExp.install("natives");
///////////////////////////////////////////////////////////////////////////////
/// LEXER
///////////////////////////////////////////////////////////////////////////////
// Create a formatToken XRegExp to match against the formatStr
var formatTokenRE = XRegExp.build("({{$token}})", {
$token: XRegExp.build("({{$user}})|({{$date}})|({{$literal}})", {
$user: /%u/,
$date: /%d/,
$literal: /.+?/
})
}, "g");
///////////////////////////////////////////////////////////////////////////////
/// PARSER
///////////////////////////////////////////////////////////////////////////////
// Build a lookup table of replacements for the tokens received that will go into the compound RegExp
var tokenREs = {
$user: /[a-zA-Z'-]+?/,
$date: /\d+-\d+-\d+/,
$literal: function($token){
return new XRegExp(XRegExp.escape($token));
}
};
// parse and convert tokens in formatStr to the replacements from tokenREs to build a compound RegExp
function parse(formatStr){
var compoundREs = [];
XRegExp.forEach(formatStr, formatTokenRE, function(match){
for(var key in tokenREs){
if(key in match && match[key] && match[key].length > 0){
var handler = tokenREs[key];
compoundREs.push(typeof(handler) == "function" ? handler(match[key]) : handler);
}
}
});
// Join compoundREs here
return XRegExp.union(compoundREs, "", "g"); // or whatever; where separator=="" here
}
///////////////////////////////////////////////////////////////////////////////
/// TEST
///////////////////////////////////////////////////////////////////////////////
// Some test data
var formatStr = "Hello %u! Today is %d.";
var testStr = "Hello AwesomeUser! Today is 09-20-2012.";
// Parse to a new RegExp and test
var formatRE = parse(formatStr);
if(!formatRE.test(testStr)){
throw new Error("Invalid input for the format string!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment