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
  • 94,763
  • 41
  • 167
  • 253
user1788736
  • 2,727
  • 20
  • 66
  • 110

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
  • 188,624
  • 52
  • 326
  • 405
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
  • 178,991
  • 47
  • 309
  • 337
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
  • 30,304
  • 2
  • 26
  • 45