Trying what I thought was a simple way to extract html tags. In this example I'm expecting an array with 4 elements but I get only one... am I missing something?

var htm="<a></a><b></b>";   
var patt = /<.*?>/gim;
var a = patt.exec(htm);
alert(JSON.stringify(a));       

Expected:

["<a>","</a>","<b>","</b>"]

But I get:

["<a>"]

Recommended Answers

All 4 Replies

Member Avatar for diafol

Can.t you just use:

document.body.getElementsByTagName('*')

You need to add parentheses around the "tags" so that they get captured. Try:
var patt = /(<.*?>)/gim;

That changes the result to ["<a>","<a>"] - makes no sense to me... it should return all matches right?

var htm="<a></a><b></b>";   
var patt = /(<.*?>)/gim;
var a = patt.exec(htm);
alert(JSON.stringify(a));       

try:

    var htm="<a></a><b></b>";   
    var patt = /(<[^>]+>)/g; //[^>] means anything that is NOT ">"
    var a = patt.exec(htm);
    alert(JSON.stringify(a));    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.