Created
January 30, 2017 17:59
-
-
Save samueljoli/38260eecc6cb590c46cd2795fdb2d4ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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