Building JavaScript arrays from HTML using regular expressions
================================================================================
Bit of idea ping-pong with Tomas (http://tmayr.com) 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('
');
}
$.each(array, function(index, value) {
$("#array").append(index + " => " + value + "
");
});
});
Nice concise bit of Javascript, after iterating through a bunch of
(over-complicated array rebuilding on my part) versions.. :D
================================================================================
Published March 05, 2011
Generated from the original post:
https://blog.omgmog.net/post/building-javascript-arrays-from-html-using-regular/
Max Glenister is an interface designer and senior full-stack developer from
Oxfordshire. He writes mostly about front-end development and technology.
- Mastodon: https://indieweb.social/@omgmog
- Github: https://github.com/omgmog
- Reddit: https://reddit.com/u/omgmog
- Discord: https://discordapp.com/users/omgmog#6206