underscore.string
underscore.string copied to clipboard
Feature: string extraction
I think this could be a useful feature.
_.extract('My name is {0} and I like {1}', 'My name is Ben and I like pie')
=> ['Ben', 'pie']
myFun = _.extract('My name is {0} and I like {1}')
myFun('My name is Ben and I like pie')
=> ['Ben', 'pie']
Sounds interesting. Do you want to make a PR?
Feels like limited version of regexp.
This is not too bad imo:
'My name is Ben and I like pie'.match(/My name is ([^ ]+) and I like ([^ ]+)/).slice(1)
# => ["Ben", "pie"]
@bsteephenson, @stoeffel, I have just opened a pull request for this: #466.
@epeli, the implementation is more complex in order to handle ordering, cases where the template string already contains regex patterns, and other edge cases...