Javascript 使用lodash链接的字符串操作
我想更改字符串Javascript 使用lodash链接的字符串操作,javascript,lodash,Javascript,Lodash,我想更改字符串“显示XXX期刊上的8868篇研究论文;发表于2000-01-01和2015-06-31之间” 致: 在XXX杂志上发表的新研究论文;自2001年1月1日起出版 我想出了以下使用lodash的代码: var desc = _.chain($('.description').text()) .thru(function (text) { return text.replace(/\s+/g, ' ') }) .thru(function (text) { retur
“显示XXX期刊上的8868篇研究论文;发表于2000-01-01和2015-06-31之间”
致:
在XXX杂志上发表的新研究论文;自2001年1月1日起出版
我想出了以下使用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()
但是我总是得到uncaughttypeerror:undefined不是一个函数.thru(函数(文本){returntext.replace(/\s+//g,,)})
我做错了什么?可能您使用的是过时的lodash版本,因为您的代码使用的是lodash 3.9.3。请注意,lodash 2中没有实现.thru
。与您的类型错误无关,您可以使用并使代码更小:
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()
您使用的是哪种版本的lodash?它似乎工作得很好