我想更改字符串"Showing 8,868 research papers in XXX Journal; published between 2000-01-01 and 2015-06-31"
于:
New research papers in XXX Journal; published from 2001-01-01 onward
我想出了以下使用lodash的代码:
var desc = _.chain($('.description').text())
.thru(function (text) { return text.replace(/\s+/g, ' ') })
.thru(function (text) { return text.replace(/Showing\s[\d+\,*]+/, 'New') })
.split(';')
.map(function (phrase) {return phrase.replace('between', 'from').replace(/and\s[\d+-.]+/, 'onward') })
.join(';')
.value()
但我总是在Uncaught TypeError: undefined is not a function
.thru(function (text) { return text.replace(/\s+/g, ' ') })
我做错了什么?
答案 0 :(得分:2)
可能你使用过时版本的lodash,因为你的代码适用于lodash 3.9.3。请注意,在{lodash 2。*
中未实现_.thru
答案 1 :(得分:1)
与您的TypeError
无关,您可以使用method()和flow()来缩小代码:
function replace(a, b) { return _.method('replace', a, b); }
_($('.description').text())
.chain()
.thru(replace(/\s+/g, ' '))
.thru(replace(/Showing\s[\d+\,*]+/, 'New'))
.split(';')
.map(_.flow(replace('between', 'from'), replace(/and\s[\d+-.]+/, 'onward')))
.join(';')
.value()