2

I suck at regexes so need some help with this, if I pass the below string into someFunction(str)

<table width="100%" border="0" cellspacing="2" cellpadding="2" bgcolor="red"> 

how can I get back an array like so:

arr[table] = "";
arr[width] = "100%";
arr[border] = "border";
arr[cellspacing] = "cellspacing";
arr[bgcolor] = "cellpadding";

or in other words, the first part of the array the tag name and the other parts its properties.

Thanks!

5
  • 2
    If you have access to the element in question as a DOM object, use it, instead of taking a string representation of it and trying to parse it. Commented Aug 1, 2011 at 14:12
  • I dont, I just have the above string and the reason I am doing this is so that I can convert it into a DOM object... Commented Aug 1, 2011 at 14:15
  • 2
    regular expression to extract all the attributes of a div Commented Aug 1, 2011 at 14:15
  • @nobody, close, but not getting the tag name... Commented Aug 1, 2011 at 14:27
  • 1
    you can get the tag name with a separate regex like this: /<\s*(\w+?)/ (res[1] will contain the tag name) Commented Aug 1, 2011 at 14:34

4 Answers 4

2

This is the way I would handle it:

var str = '<table width="100%" border="0" cellspacing="2" cellpadding="2" bgcolor="red">'
var arySplits = str.split(' ');
var aryFinalVals = new Array();

for (var i = 0; i < arySplits.length; i++) {
    var arySubSplits = arySplits[i].split('=');
    aryFinalVals[arySubSplits[0].replace(/</, '')] = (arySubSplits[1]) ? arySubSplits[1].replace(/"/g, '').replace(/>/, '') : '';
}

Summary:

  1. Split string using the spaces.
  2. Loop through the array of property pairs and split by the =.
  3. Remove the double quotes and closing tag.
Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense there, but how to get the tag name as well?
0

You can try this:

/([a-z]+)="([a-z0-9%]+)"/g

This is quick and dirty, though, not very resilient to variations... more "blindly":

/([^=]+)=("[^"]+")/g

the latter one will match everything that is not = just preceding a =, then ignore the = and match what is between ".

Comments

0

If you just want to convert a string to a DOM object, you should be able to use innerHTML to invoke the browser's HTML parser:

var strToObj = function(str) {
    var el = document.createElement('div');
    el.innerHTML = str;
    return el.childNodes[0];
}
document.body.appendChild(strToObj('<table width="100%" border="0" cellspacing="2" cellpadding="2" bgcolor="red">'))

1 Comment

Not allowed to use innerHTML :(
0

A quick and dirty proof of concept:

var input = '<table width="100%" border="0" cellspacing="2" cellpadding="2" bgcolor="red">';
var properties = {};
var tempNode = document.createElement("div");
tempNode.innerHTML = input;

for(var p in tempNode.firstChild.attributes){
    if( tempNode.firstChild.attributes.hasOwnProperty(p) ){
        properties[tempNode.firstChild.attributes[p].name] = tempNode.firstChild.attributes[p].value;
    }
}

It's not fully tested and it makes lots of assumptions, but I hope it can give you an idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.