views:

256

answers:

5

I have a string like this:

var str="<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title></title></head><body><table><tr><td style="color:#FF0000">demo1</td><td style="color:#FF0000">demo2</td></tr></table></body></html>";

I have to get the contents inside all td elements alone using regular expression in JavaScript. My output should be:

var str="demo1 demo2"

How to do this? Any idea?

+2  A: 

what about

var str = document.body.innerText || document.body.textContent;
rahul
I am getting that string from responseText using ajax.. How can i use document.body.innerText or document.body.textContent.. I dont know that i can use this with that. .
@senthil: You can't.
Guffa
+1  A: 

Use a regular expression like /<td[^>]*>(.*?)</td>/ to get the contents of the cells, then concatenate them with spaces between.

Guffa
A: 
var str='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title></title></head><body><table><tr><td style="color:#FF0000">demo1</td><td style="color:#FF0000">demo2</td></tr></table></body></html>';
var resultM=new Array(), pattern=/<td[^>]*>(.+)<\/td>/, match;
while(match=pattern.exec(str))
{
resultM.push(match[1]);
}
var result=resultM.join(" ");
mck89
Hi, I got the required o/p if i use your code as above.But i dont get o/p if i use like this .. var str=req.responseText; i am forming that string from ajax response by reading a html file. what may be the error... ??? Plz let me know
+2  A: 

Putting it all together you'll get this one:

var str='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title></title></head><body><table><tr><td style="color:#FF0000">demo1</td><td style="color:#FF0000">demo2</td></tr></table></body></html>';
var resultM=[], pattern=/<td[^>]*>(.*?)<\/td>/g, match;
while(match=pattern.exec(str))
{
resultM.push(match[1]);
}
var result=resultM.join(" ");
console.log(result); // demo1 demo2
bjoernwibben
Nice code ...Thanks
Hi,I got the required o/p if i use your code as above.But i dont get o/p if i use like this ..var str=req.responseText; i am forming that string from ajax response by reading a html file.what may be the error... ??? Plz let me know var resultM=[], pattern=/<td[^>]*>(.*?)<\/td>/g, match; while(match=pattern.exec(str)) { resultM.push(match[1]); } var result=resultM.join("\n"); alert(result);
hi, can you please show us what the output of str looks like right after str=req.responseText? thx
bjoernwibben
+1  A: 

You can't reliably parse HTML with regexp. Looking for “<td[^>]*>” might work for the example you gave, but anything out of the ordinary, like ‘>’ in an attribute value, or ‘<td’ in a comment, or omitted close tags, would confuse it.

The best thing to do would be to change the server-side so that it returns the data in a way that is more convenient for JavaScript to read — normally JSON.

bobince