簡體   English   中英

從文本中提取ISBN號

[英]Extract ISBN numbers from text

該Javascript從HTML表單中獲取ISBN(10-13位數字)列表,並為每個表單打開一個新標簽,其中啟動了對Amazon的搜索請求。 表格中輸入的ISBN有一個換行符,每個ISBN旁邊都有其預定條件。

我需要的Javascript功能來搜索只是 ISBN和啟動亞馬遜搜索之前解決任何格式,所以它不會破壞搜索。

在下面的表單示例中,將需要它來搜索這三個ISBN:0321973615,0 321 973 615(不帶空格)和0321973615。它包含空格,額外的數字(例如“ 12-15頁,占25%”)以及所有多余的單詞其中絕不能搜索,因為它們破壞了搜索。

0321973615 12-15 pages highlighted
0 321 973 615 good condition
13:0321973615 25% highlighting

小提琴: 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> 

如何從文本中提取ISBN號而沒有任何間距?

您可以使用以下代碼:

function handler () {
    var items = input.value.match(/\b(\d\s*?){10,13}\b/gm);
    console.log('your collection', items);
    items.forEach(function (item) {
        item = item.replace(/\D+/g, '');
        console.log('opening page for isbn ', item)
        openPage(item);
    });
}

注意:在輸入事件觸發時打開窗口是個壞主意。 如果開始在文本區域中輸入內容,則會給用戶帶來非常糟糕的體驗。 大多數瀏覽器還會在打開其他類似窗口之前發出警告。

相反,您可以生成超鏈接,僅當用戶單擊其他選項卡時,超鏈接才會打開。 鏈接指向example.com。 用您需要的替換它。

以下是執行此操作的代碼段:

 //the input box. var input = document.getElementById('numbers'); var output = document.getElementById('output') var base = 'https://www.example.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=' //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.split(/\\b((?:\\d\\s*?){10,13})\\b/gm); // Build DOM for output var container = document.createElement('span'); items.map(function (item, index) { if (index % 2) { // it is the part that matches the split regex: var link = document.createElement('a'); link.textContent = item.trim(); link.setAttribute('href', base + item.replace(/\\D+/g, '')); container.appendChild(link); } else { // it is the text next to the matches container.appendChild(document.createTextNode(item)) } }); // Replace output output.innerHTML = ''; output.appendChild(container); } handler(); // run on load 
 <div><b>ISBN Hyperlinker</b></div> <textarea id=numbers placeholder="paste isbn numbers as csv here" style="width:100%"> 0321973615 12-15 pages highlighted 0 321 973 615 good condition 13:0321973615 25% highlighting </textarea> <div><b>Hyperlinked text:</b></div> <div id="output" style="white-space: pre"></div> 

逐幀運行

如果某些搜索網站以框架形式加載,則不會呈現。 您可以通過在代碼中添加以下行來指示應在新窗口/標簽頁中打開它們:

      link.setAttribute('target', '_blank');

這在SO代碼段中不起作用,因此我將其省略。

關於ISBN格式

上面使用的正則表達式回答了您的問題10-13位數字 ,但是正如注釋中所述,ISBN代碼可能以X結尾。 請參閱此答案 ,其中包括更復雜的正則表達式,同時考慮了潛在的最終X

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM