从文本中提取ISBN号

Extract ISBN numbers from text

本文关键字:ISBN 提取 文本      更新时间:2023-09-26

此Javascript从HTML表单中获取isbn(10-13位数字)列表,并为每个列表打开一个新选项卡,在其中启动对Amazon的搜索请求。在表格中输入的ISBN有一个换行符,每个ISBN旁边都有它的图书状况描述。

我需要Javascript在启动Amazon搜索之前搜索仅搜索的isbn并修复任何格式,因此它不会破坏搜索。

对于下面的表单示例,它将需要它来搜索这三个isbn: 0321973615、0321973615(不带空格)和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