Building JavaScript arrays from HTML using regular expressions
Bit of idea ping-pong with Tomas this evening, to provide a solution to something he is working on. He had some text in a table cell that needed to be split and put into an array, separating each line into two parts.
This is the snippet we came up with, to build a flat array
$(function() {
var array = $("p.src").html()
.split(/([0-9]{1})\s([A-Za-z\s]+\s*[0-9°º]*)/);
var position = 0;
while (position != -1) {
array.splice(position, 1);
position = array.indexOf('<br>');
}
$.each(array, function(index, value) {
$("#array").append(index + " => " + value + "<br />");
});
});
Nice concise bit of Javascript, after iterating through a bunch of (over-complicated array rebuilding on my part) versions.. :D
Related posts
- Using jQuery to create a custom thumbnail for an embedded video
Recently Jenny asked me for a way to embed a YouTube video with a custom thumbnail that would play the video when clicked.
- Android Auto on my HR-V's Android 4.0.4 head unit
I recently bought a 2018 Honda HR-V as our family car. It has a 7” touchscreen running Android 4.0.4, a version so old it predates Android Auto by about three years. It runs Android Auto now anyway, thanks to a small app I built over the course of a week.
- Rebuilding and analysing 4 years of Wordle stats from WhatsApp chat logs
Yes, I’m still playing Wordle in 2026. According to Wordle, I’ve played 1,559 games, I win 99% of the time, my current streak is 107 days and my longest is 278. Four numbers, and they’re mostly correct. What I don’t get is anything behind them. No per-game history. No way to compare a Tuesday to a Sunday. No sense of whether I’ve actually got better at this over four years. Each day I’ve played, I’ve also shared the result to WhatsApp. Just a habit, not a data strategy, but it turns out that habit quietly built a far more detailed record than Wordle itself ever keeps. One I can finally ask my own questions of. So I looked into rebuilding my own copy of the data.
Discussion
No interactions yet
Join the conversation
Reply from your own site with a Webmention (how?).
