pwntastic Posted July 24, 2013 Share Posted July 24, 2013 Hello, I have some html in a string from an AJAX response. I'm currently trying to filter certain information. I tried to extract rows from the table with a regular expression but the problem is the table is made something like this: <table> <tbody> <tr> <td>DATA</td> <td>DATA</td> <td><table><tbody><tr><td>DATA</td></tr><tr><td>DATA</td></tr></tbody></table></td> </tr> My regular expression is: <tr>[^()]*<\/tr> This gets the first tr through like the second </tr> in the nested table though. Does anyone have any suggestions with the regular expression or even a better way to go about this? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 24, 2013 Share Posted July 24, 2013 (edited) even a better way to go about this? if this is your application, the data should be output in a format that supports how you are using the data, not make it harder, requiring more processing before you can even use the data. you should output the data as a JSON object with key/values that let you directly access the data (you should only output the data you want as well.) Edited July 24, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 24, 2013 Share Posted July 24, 2013 (edited) And, if this is returned from an AJAX response that you do not control, then the better approach would be to use DOM parsing. Edited July 24, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
pwntastic Posted July 24, 2013 Author Share Posted July 24, 2013 I have no control of the output since its not my application. I just have to use that data. Quote Link to comment Share on other sites More sharing options...
pwntastic Posted July 24, 2013 Author Share Posted July 24, 2013 I'm not too familiar with this. I tried something simple like this to try to get the hang of it but I get weird results. I've been googling this around and have come across similar for loops to put the data in. <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> window.onload = function() { var data = document.body.innerHTML; var div = document.createElement('div'); div.innerHTML = data; var matches = new Array(); for (var i = 0; i < data.length; i++) { matches.push(data[i]); } } </script> </head> <body id="body_content"> <div> hello <div> hello <div> hello </div> </div> </div> </body> </html> in this code I get data.length = 203 Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 24, 2013 Share Posted July 24, 2013 (edited) What data EXACTLY are you wanting from that content? All you said is you want to "filter certain information". Also, I just realized you are doing this in JS, not PHP. So, you can try to build the functionality in JS OR you could do a 2nd AJAX call and pass the content to a page you create that will parse the data from the first call and send it back in the format you need. Edited July 24, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
pwntastic Posted July 24, 2013 Author Share Posted July 24, 2013 Yes, the thing is I have a table with prices, item names, item options, and other item related info from the ajax request. I'm trying to get this information, and display it in a div but I can't display it the way it is in the table. So I would like to extract the information, and place it in my own layout. The code displayed last by me, is only a demonstration of one way I was trying to do this but I have failed miserably. Quote Link to comment Share on other sites More sharing options...
pwntastic Posted July 25, 2013 Author Share Posted July 25, 2013 Hello, I figured out I can use $.parseHTML() or DOMParser(). I can't use $.parseHTML because the code I'm working on uses jquery 1.7.2 (using the current vers. causes problems with some other parts of the page). For some reason when I use DOMParser, seems to work on chrome and fire fox but not on ie. I've tried a simple example though in ie with the DOMParser and that worked well. Why would it not work on ie? I believe it should work in ie8+. Also, another problem I found is that ff and ie read my regular expressions differently. My regular expression is like so: \<tbody\>[\s\<\>a-zA-Z\=\"\:\/\d\.\-\_\&\;\[\]\,\#\(\)\'\?$$]*\<\/tbody\>/g In chrome this expression returns all the code between tbody tags. In ff and ie it only returns the beginning and ending tbody tags and some space in between. Why is this? Quote Link to comment Share on other sites More sharing options...
Solution pwntastic Posted July 26, 2013 Author Solution Share Posted July 26, 2013 I've figured out the problem. Was pretty silly on my part. It wasn't displaying information on other browsers because there was no information in the cart for those browsers. I apologize for this and I'm thankful for those who attempted to help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.