1

All I got is an HTML string from XMLHttpRequest and I posted it to a textbox.

document.myform.outputtext.value = xhr.responseText;

But I only want part of that big HTML string that is between </fb:send> and <div class="styles">. I have done similar things in PHP but don't know how I can do that in JavaScript? All I want is the xhr.responseText part that is between </fb:send> and <div class="styles">. I'd be happy if some one help me with this. Thanks.

PHP example:

$start = strpos($file_contents, '</fb:send>');
$end = strpos($file_contents, '<div class="styles">', $start);
$code = substr($file_contents, $start, $end);
David G
  • 90,891
  • 40
  • 158
  • 247
user1788736
  • 2,587
  • 18
  • 62
  • 107

3 Answers3

2
var start = xhr.responseText.indexOf('</fb:send>');
var end = xhr.responseText.indexOf('<div class="styles">', start);
var code = xhr.responseText.substring(start, end);
Explosion Pills
  • 183,406
  • 48
  • 308
  • 385
2

Regexes are cool.

var html = "foo</fb:send>bar<div class=\"styles\">baz";
var code = html.match(/<\/fb\:send>(.*?)<div class="styles">/)[1];
console.log(code); // 'bar'

Just don't expect them to do the job of an HTML parser.

Community
  • 1
  • 1
Alex Wayne
  • 162,909
  • 46
  • 287
  • 312
0

Have you tried indexOf and substring? Here is an example:

<script>
var stringTxt = "123abcxyz890";
var start = stringTxt.indexOf("a");
var end = stringTxt.indexOf("z", start);
var newStringTxt = stringTxt.substring(start, end);
</script>
Twisty
  • 27,669
  • 1
  • 25
  • 43