Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Created October 5, 2012 05:35
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 hengkiardo/3838274 to your computer and use it in GitHub Desktop.
Save hengkiardo/3838274 to your computer and use it in GitHub Desktop.
A simple helper to extract values from a string based on a pattern.
var extractValues = function(str, pattern, options) {
options = options || {};
var delimiters = options.delimiters || ["{", "}"];
var lowercase = options.lowercase;
var whitespace = options.whitespace;
var special_chars_regex = /[\\\^\$\*\+\.\?\(\)]/g;
var token_regex = new RegExp( delimiters[0] + "([^" + delimiters.join("") + "\t\r\n]+)" + delimiters[1], "g");
var tokens = pattern.match(token_regex);
var pattern_regex = new RegExp(pattern.replace(special_chars_regex, "\\$&").replace(token_regex, "(\.+)"));
if (lowercase) {
str = str.toLowerCase();
}
if (whitespace) {
str = str.replace(/\s+/g, function(match) {
var whitespace_str = "";
for (var i = 0; i < whitespace; i++) {
whitespace_str = whitespace_str + match.charAt(0);
};
return whitespace_str;
});
};
var matches = str.match(pattern_regex)
if (!matches) {
return null;
}
matches = matches.splice(1);
var output = {};
for (var i=0; i < tokens.length; i++) {
output[tokens[i].replace( new RegExp( delimiters[0] + "|" + delimiters[1], "g"), "")] = matches[i];
}
return output;
}
/*
Examples
extractValues("/2012/08/12/test.html", "/{year}/{month}/{day}/{title}.html")
>> { "year": "2012", "month": "08", "day": "12", "title": "test" }
extractValues("John Doe <john@example.com> (http://example.com)", "{name} <{email}> ({url})")
>> {"name": "John Doe", "email": "john@example.com", "url": "http://example.com" }
extractValues("from 4th October to 10th October", "from `from` to `to`", { whitespace: 1, delimiters: ["`", "`"] })
>> {"from": "4th October", "to": "10th October" }
extractValues("Convert 1500 Grams to Kilograms", "convert {quantity} {from_unit} to {to_unit}", { lowercase: true })
>> {"quantity": "1500", "from_unit": "grams", "to_unit": "kilograms" }]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment