Hey guys I have a web page where I want to capture all numbers enclosed between certain tags. Now when I use RegEx it returns the whole string with tags but I only want numbers. Here is my script -: alert(document.body.innerHTML.match(/<td class="ac" style="padding: 0px 5px;">([0-9]+)<\/td>/g)); Code (markup): What it returns... <td class="ac" style="padding: 0px 5px;">126</td>, <td class="ac" style="padding: 0px 5px;">941</td>, <td class="ac" style="padding: 0px 5px;">23</td>, Code (markup): What I want.... 126 941 23 Code (markup):
this is very ineffective. granted, you could refine the regex or iterate the array and strip the html but this is not practical. it's slow and relies on you not changing your html ever. not sure what framework you use but if your mark-up goes like so: <table id="mydata"> <tbody> <tr> ... some more data other tds, other classes <td class="ac" style="padding: 0px 5px;">126</td> </tr> <tr> ... some more data other tds, other classes <td class="ac" style="padding: 0px 5px;">941</td> </tr> ... some more data other tds, other classes <td class="ac" style="padding: 0px 5px;">23</td> </tr> </tbody> </table> // then use a selector. this is in mootools, its similar in jquery. var values = []; $("mydata").getElements("td.ac").each(function(el) { values.push(el.get("text").trim()); }); PHP: watch it in action: http://mooshell.net/3qXn8/ why it's better? it first of all gets the parent object which is the table. then it finds childnodes of that by tagname TD and filters those with class AC. this is faster than traversing all the body source and using regex. the only thing that matters here is to be able to assign a unique enough CSS selector. if your table has no id, then don't call it as the parent, you can always do $(document.body).getElements (or a broader $$(".ac") - it's a matter of speed). if you can't change the class of the table cells, you can refernece it's TRs and then call n-th child for your cells. read up on selectors. incidentally you can use split instead of match in your regex, then filter out the array and remove empty values (it will return ["", "126", "", "194", "", "23" ... ])