Skip to content

Instantly share code, notes, and snippets.

@samueljoli
Created January 30, 2017 17:59
Show Gist options
  • Select an option

  • Save samueljoli/38260eecc6cb590c46cd2795fdb2d4ba to your computer and use it in GitHub Desktop.

Select an option

Save samueljoli/38260eecc6cb590c46cd2795fdb2d4ba to your computer and use it in GitHub Desktop.
//javascript
var myRegexFunctionMap = {
"/qty: (\d+)/": function (obj, matches) {
obj.quantity = matches[1];
},
"/size: (\d+)x(\d)/": function (obj, matches) {
obj.width = matches[1];
obj.height = matches[2];
},
//etc
}
//main function
var object = new MyObject();
for(line in file) { //Assume that this reads each line.
for(key in myRegexFunctionMap) {
var regex = new RegExp(key);
var matches = line.matches(regex);
matches && myRegexFunctionMap[key](object, matches);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment