简体   繁体   English

Javascript-数据过滤和删除换行符

[英]Javascript - data filtering and removing line breaks

A list of isbn's are entered in the text area and javascript opens an amazon search for each one entered. 在文本区域中输入了一个isbn列表,而javascript会为每个输入的内容打开一个Amazon搜索。

Isbn's can be between 10 or 13 digits. Isbn可以介于10或13位数字之间。 Not all isbn searches start with an isbn, sometimes there can be information in front such as "isbn10:0195433831" so you cant count from the start. 并非所有的isbn搜索都以isbn开头,有时前面可能会包含诸如“ isbn10:0195433831”之类的信息,因此您不能一开始就计算在内。

For Example, this is a typical search: 例如,这是一个典型的搜索:

0195433831 Good 0195433831好

0195433831 Poor 0195433831差

0195433831 Excellent 0195433831很棒

Question

Often times isbn's are entered in a different format breaking it, for example: 通常,isbn是以不同的格式输入的,例如:

1) With spacing between numbers 1)数字之间有间距

978 0 132 76682 1 like new 978 0 132 76682 1喜欢新

978 0 495 38500 4 very good 978 0 495 38500 4非常好

2) With additional rating numbers added creating additional unnecessary searches. 2)添加了附加的评分编号,从而创建了其他不必要的搜索。

9781118624616 9/10 condition with minimal highlighting 9781118624616 9/10条件,突出显示最少

9780415462020 10/10 condition, brand new 9780415462020 10/10条件,全新

So I must find a way to have Javascript filter out these conditions. 因此,我必须找到一种使Javascript过滤掉这些条件的方法。

Code is here: 代码在这里:

 //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.com/gp/search/ref=sr_adv_b/?search-alias=stripbooks&field-isbn=' window.open(base + isbn) } 
 <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> 

ISBNs should be 13 numbers if I remember right so can't you just remove the whitespace and match groups of 13? 如果我没记错的话,ISBN应该是13个数字,所以您不能删除空白并匹配13个数字的组吗?

var str = "978 111 862 4616 9/10 condition with minimal highlighting\n\n9780415462020 10/10 condition, brand new",
    nums = str.replace(/\s/,"").match(/\d{13}/g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM