This Javascript grabs lists of ISBNs (10-13 digit numbers) from an HTML form and opens a new tab for each one, in which a search request on Amazon is launched. ISBNs entered in the form have a line break and each ISBN has its book condition described next to it.
I need the Javascript to search JUST the ISBNs and fix any formatting before launching the Amazon search, so it doesn't break the search.
With the form example below, it will need it to search these three ISBNs: 0321973615 , 0 321 973 615 (without spaces) and 0321973615. It includes spaces, extra numbers like "12-15 pages, 25%", and extra words all of which must not be searched, because they break the search.
0321973615 12-15 pages highlighted
0 321 973 615 good condition
13:0321973615 25% highlighting
Fiddle: https://jsfiddle.net/09vfmhep/1/
//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(/\s/g, '').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>
How can I extract the ISBN numbers from the text, without any spacing?