2

I'm about to trying to parse a tag from the text returned by a XMLHttpRequest but I can't get it to work. I've tested it in w3schools editor (http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_i) where I can it to to work when the text has no newlines but else im stuck:

<script language="javascript" type="text/javascript">

var req = new XMLHttpRequest();  
req.open('GET', 'www.mysite.com/index.html', false);   

req.onreadystatechange=function() {

   if (req.readyState==4) {
      var text = req.responseText;
      var tag = /<div class='classdef'>(.*?)<div/gm;
      var mt = tag.exec(text);
      alert(mt);
      return;
   }
};

req.send(null);  

</script>

At the best the alert box just writes the raw regex "/(.*?)

Anyone got a pointer to what I'm doing wrong? :)

Kind regards.

UPDATED SOLUTION

Thx for all your advice, it seems there where more than just one thing amidst in my code. My goal was to feed data into a section of a page from another page. I ended up using jquery as you suggested to achieve this:

<div id="IdOfTagToAddTo"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script>

$('#IdOfTagToAddTo').load('somePageInMyDomain.html #SomeIdOfATag');

</script>

I realise that I should have made my initial goal more clear. :S. For that im sorry as I know that always causes confusion :(. Thx a million for all the pointers you guys gave me. I'm entering the new world (for me at least :)) of jQuery from now on :).

Kind regards

Christian Mikkelsen
  • 1,661
  • 2
  • 19
  • 44
  • 4
    I'd recommend using any site other than w3schools. [JSfiddle](http://jsfiddle.net) is good for testing out javascript. – Andrew Marshall Feb 20 '11 at 09:06
  • What exactly are you trying to match (and what do you need to not match)? Do you have to deal with nested tags (if so a single regex probably won't do the job)? What do you expect `exec` to return? – jswolf19 Feb 20 '11 at 09:14
  • 1
    Please refer to accepted answer here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Miquel Feb 20 '11 at 09:24
  • 1
    I think this "answer" is off-topic, so I'll post it as a comment: You're doing a synchronous request (the third parameter to `open` is `false`), but you're using `onreadystatechange`. That will not work reliably cross-browser, and MDC specifically says not to use `onreadystatechange` with synchronous requests. I'd strongly recommend using an *asynchronous* request instead, as synchronous requests lock up the UI of many browsers, making for an unpleasant user experience. So either do that (change the `false` to `true`), or if the request *has* to be synchronous, don't use `onreadystatechange`. – T.J. Crowder Feb 20 '11 at 09:24

2 Answers2

1

exec method returns array. The first (0 index) is your match, and second (index 1) is match for your group. You should take second element from it.

var mt = tag.exec(text);
alert(mt[1]);

And add slash to your regex.

But parsing html with regex is veryb bad idea. Use something else, jquery for example.

And remember, that you cannot issue XMLHttpRequest to site in other domain.

gor
  • 11,498
  • 5
  • 36
  • 42
1

Expanding on gor's anwser.

Using jQuery:

  $(req.responseText).find('div.classdef').text()

Using split (no regexps), somewhat cryptic:

req.responseText.split("<div class='classdef'>")[1].split('</div>')[0]
Alexey Lebedev
  • 11,988
  • 4
  • 39
  • 47
  • It took a while as I have never ever done any jquery before but I ended up using load(). I've written the complete solution in my original question and are marking your post as the answer :). Thx for pointing me to jquery, brave new world ahead :). – Christian Mikkelsen Feb 20 '11 at 16:52