Puisque vous mentionnez jQuery, sélectionnez simplement tous les noeuds droite et vérifier leur texte. Vous pouvez mettre une regex ici si vous voulez, mais ce n'est pas nécessaire.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){
var texts = [];
$('b.blah').each(function()
{
var txt = $(this).text();
if (0 == txt.indexOf('Blah:'))
{
texts.push(txt.substr(6));
}
});
alert(texts);
});
</script>
</head>
<body>
<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>
<div id="foo"><b class="blah">Some Text 3</b>
</body>
</html>
Ou avec une chaîne de HTML
$(function(){
var htmlChunk = '<b class="blah">Blah: Some Text 1</b>\n'
+ '<div id="foo"><b class="blah">Blah: Some Text 2</b></div>\n'
+ '<div id="foo2"><b class="blah">Some Text 3</b></div>';
var texts = [];
$('b.blah', '<div>' + htmlChunk + '</div>').each(function()
{
var txt = $(this).text();
if (0 == txt.indexOf('Blah:'))
{
texts.push(txt.substr(6));
}
});
alert(texts);
});
j'ai oublié cette solution. Toutefois, le code HTML que je veux analyser est stocké sous la forme d'une chaîne (c'est-à-dire que mon extrait html est stocké dans une variable javascript). Jquery peut-il analyser une chaîne comme un fichier html? –
Ouais - ajouté ci-dessus. Je ne sais pas à quel point c'est efficace avec de gros morceaux de HTML. –