JSFiddle - React, Tailwind, and code Playground
by lollero
HTML
<div class="string">
Series:
<a href="#url">Example</a>, Book 1,
<a href="#">Lorem (Ipsum)</a>, Book 4.5,
<a href="#">Jimmy two legs</a>,
<a href="#">The case of the missing, boot</a>, Book 0.5,3.7,4
</div>
JavaScript
String.prototype.trimAll = function(){
return this.trim().replace(/\s+/g,' ');
}
var string = $('.string').html().trimAll();
string = string.substring(string.indexOf(':')+1).trim();
// string = string.match(/<a(.*?)<\/a>,(*,)/g);
string = $.parseHTML(string);
var series = [];
$.each( string, function( index, object ) {
var string = object.textContent.trim().replace(/^\,/,'').trim().replace(/\,$/, "");
var titleRow = (index+1) % 2;
var numberRow = !titleRow;
if ( titleRow ) {
series.push({
name: string,
url: object.href
});
}
else if ( numberRow ) {
if ( string.match(/\d/) ) {
// Trims text from the front ("Book ") and splits numbers separated by commas
var numbers = string.replace(/^[^0-9]*/g, '').trim().split(',');
// Numbers are added to the previous item
var lastItem = series[ series.length-1 ];
lastItem.bookNumbers = jQuery.map( numbers, function( n, i ) {
return parseFloat( n );
});
}
}
});
console.table( series );