Created
October 5, 2012 05:35
-
-
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.
This file contains 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
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 <[email protected]> (http://example.com)", "{name} <{email}> ({url})") | |
>> {"name": "John Doe", "email": "[email protected]", "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