[英]String manipulation using lodash chaining
我想更改字符串"Showing 8,868 research papers in XXX Journal; published between 2000-01-01 and 2015-06-31"
to: New research papers in XXX Journal; published from 2001-01-01 onward
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
行上的Uncaught TypeError: undefined is not a function
.thru(function (text) { return text.replace(/\\s+/g, ' ') })
我究竟做錯了什么?
可能你使用過時版本的lodash,因為你的代碼適用於lodash 3.9.3。 需要注意的是_.thru
不lodash 2.實施*
與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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.