underscore.string icon indicating copy to clipboard operation
underscore.string copied to clipboard

Feature: string extraction

Open bsteephenson opened this issue 9 years ago • 3 comments

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']

bsteephenson avatar Mar 05 '15 18:03 bsteephenson

Sounds interesting. Do you want to make a PR?

stoeffel avatar May 02 '15 09:05 stoeffel

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"]

esamattis avatar May 04 '15 13:05 esamattis

@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...

Belema avatar Dec 07 '15 16:12 Belema