tags:

views:

22

answers:

2

Hi,

I'm retrieving data from a form, using Jquery's ajax method. The data is coming back fine, and I can retrieve individual div elements by id or class.

My problem is this - within one div I want to retrieve specific text occuring between two constant anchor words; this text occurs right at the start of the div. I've created a string with all the text from the div, and I'm guessing that I can approach it in either of two ways. The first would be to run a .match() with a correct regular expression, or alternatively run a .replace() to delete all text after the end anchor word. I'm stumped for the format of the regular expression though - or perhaps I'm approaching this the wrong way?

So, for example:

success: function(data) {
    $('#result').html("<div id='message'></div>");
    $('#message').html("<h2>Your Results</h2>");
    $(data).find('.Status').appendTo('#message');
   toreplace= $(data).find('.Results').text();

  textresult = toreplace.match();
  $('#message').append(textresult);
}

The text in question looks like this

Results Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Fusce iaculis ullamcorper egestas.
Sed congue imperdiet sem et interdum.
Aliquam erat volutpat.
Nulla facilisi.
Duis in nisi arcu, non ullamcorper nulla.
Sed vel est vitae nunc volutpat ullamcorper.
Quisque semper fermentum quam ac dapibus. Quisque orci augue, cursus et dignissim nec, consequat non metus.

So I want to grab all the text between 'Results' and 'Sed'.
Any help would be much appreciated.

A: 

Regex Results(.*)Sed would help you, but in JS, dot, does'nt match newlines, so before parsing text for occurances, you must replace \r\n by something (that you wold replace back to \r\n in future) or just delete occurances of \r\n.

Bick
A: 

You can do this easily with a string split:

var txt = $('.post-text').html().split("<strong>Results</strong>")
var txt1= txt[1].split("<strong>Sed</strong>")
var result = txt1[0]
Diodeus