此代码使用ISBN搜索查询Amazon,如"128584632X“。它不工作与间隔isbn的,所以我需要让他们过滤与删除的空格,如"12,858,463 2X“。我需要"var项“对空格进行过滤。
JSFiddle这里:https://jsfiddle.net/jfjd8a3h/
//the input box.
var input = document.getElementById('numbers');
//adding an event listener for change on the input box
input.addEventListener('input', handler, false);
//function that runs when the change event is emitted
function handler () {
var items = input.value.replace(/\r?\n/g, ' ').split(' ');
length = items.length;
console.log('your collection', items);
for (var i = 0; i < length; i++) {
if ( items[i] && !isNaN(items[i]) ) {
console.log('opening page for isbn ', items[i])
openPage(items[i]);
}
}
}
//opens the tab for one isbn number
function openPage (isbn) {
var base = 'https://www.amazon.ca/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords='
window.open(base + isbn)
}
<h1>Amazon Bulk ISBN Search</h1>
<p>... note, after paste you may need to click outside the text area or tab out to fire the change event.</p>
<textarea id=numbers placeholder="paste isbn numbers as csv here">
</textarea>
发布于 2016-09-10 05:18:05
我觉得你想要的是:
var items = input.value.replace(/\r?\n/g, '!').replace(/\s/g, '').split('!')
首先,我不得不使用!
作为换行符的替换,因为似乎\s
也在对换行符进行条带化。
至少这会让你开始。
另一种方法可能是首先在换行符上使用拆分,然后对集合中的每个项运行替换。我想会更容易读懂。
编辑:这个条件阻止打开页面的代码运行,因为ISBN以X结尾,所以它不是一个数字:&& !isNaN(items[i])
https://stackoverflow.com/questions/39422693
复制相似问题