views:

357

answers:

3

In JavaScript/JQuery I want to get all the text that is seen between some other text. For example, if the HTML document had:

<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>

I'd like to get an array that has 'Some Text 1' and 'Some Text 2' since they are both in between '<b class="blah">Blah: ' followed by a '</b>'

A: 

This is kind of hard in JS, because there's no handy way to retrieve a global set of paren captures. A hack like this might work:

var chunked = text.replace(/.*<b class="blah">(.*?)<\/b>/g, '$1|ARBITRARY_SEPARATOR|');
var chunks = chunked.split(/|ARBITRARY_SEPARATOR|/);
chunks.pop();
chaos
For some reason, this just pops out "|" (the last character in the string). Am I doing it wrong?
Hunh. Are you by any chance using the result of `chunks.pop()`? What I meant is that you should use what's left in `chunks` after you do the `pop()`.
chaos
A: 

Since you mention jQuery, just select all the right nodes and check their text. You can put a regex in here if you want, but it's not needed.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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"&gt;&lt;/script&gt;
<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>

Or with a string of 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 );
});
Peter Bailey
i forgot about this solution. However, the html that i want to parse is stored as a string(i.e. my html snippet is stored in a javascript variable). Can Jquery analyze a string as it does an html file?
Yeah - added it above. Not sure how efficient it is with really large chunks of HTML.
Peter Bailey
A: 

This code will produce an array with the text between '<b class="blah">Blah: ' and '</b>'. in this example 'Some Text 1' and 'Some Text 2'

var s = '<b class="blah">Blah: Some Text 1</b><div id="foo"><b class="blah">Blah: Some Text 2</b>';

var regex = /<b class="blah">Blah: (.+?)<\/b>/gi;
var result = [];
var e;
while (e = regex.exec(s))
{
  result.push(e[1]);
};
port-zero
While iteration never stops ...
Guido