Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Last active November 1, 2017 11:50
Show Gist options
  • Save ishan-marikar/f02b343b4112ae7fb3a810a28cfb1110 to your computer and use it in GitHub Desktop.
Save ishan-marikar/f02b343b4112ae7fb3a810a28cfb1110 to your computer and use it in GitHub Desktop.

I have a question, and it's something I've been trying to learn for a while (and if I can't find something like it, I guess I'll have to build it).

I'm looking for something where I can have a template with a standardized format that would look a little something like this:

let x = new Extracter("Account: {{accountNumber}}, Location: {{location}}, Date: {{date}}, Amount: {{amount}}");

... where we could pass in strings that would fit that format and we could extract the data out of the strings:

let data = x.parse("Account: 12345, Location: MC, Date: 12/12/12, Amount: 123.00");

console.log( JSON.stringify( x, null, 2) );

/*

{
    "accountNumber": "12345",
     "location": "MC",
     "date": "12/12/12",
     "amount": "123.00"
}

*/

I'm aware that this is possible via regular expressions though using named capture groups (it's not as pretty as this, but gets the job done), but AFAIK, there is no such thing in javascript.

It would be amazing to have something like this because it would make log parsing so much easier.

EDIT: http://xregexp.com/ hits close to the mark. Going to try it out and see what it can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment