Question: I have tag like this, how the best way to get every key and value of those attribute and populate it within an array (number of attribute will be increasing)?

```
myData = '[data attr1="value1" attr2="value2" attr3="value3"]';

```

and get result array :

```
var arr = new Array();
arr['attr1'] = "value1";
arr['attr2'] = "value2";
arr['attr3'] = "value3";
and so on...

```

Answer: 
This probably does what you want, though it assumes that `tag` is already in the format you have described, i.e. a singular occurrence of `[data ... ]`.

Also, the regular expression is purely based on what I've seen in your question; not sure whether it will break on other strings.

```
function decode(tag)
{
    var r = /(\w+)="([^"]*)"/g,
    h = {};
    while ((m = r.exec(tag)) !== null) {
        h[m[1]] = m[2];
    }
    return h;
}

```
```
var tagRe = /\[(\w+)((?:\s+\w+="[^"]{0,50}")*)\s*]/g;
var attrRe = /\b(\w+)="([^"]*)"/g;

function parse(text) {
    var result = [];
    tagRe.lastIndex = 0; // reset start position

    var tagMatch = tagRe.exec(text);
    while (tagMatch) {
        var currentTag = { 'name': tagMatch[1], 'attrs': {} };

        var attrString = tagMatch[2];
        attrRe.lastIndex = 0;

        var attrMatch = attrRe.exec(attrString);
        while (attrMatch) {
            var attrName = attrMatch[1];
            var attrValue = attrMatch[2];

            currentTag.attrs[attrName] = attrValue;

            attrMatch = attrRe.exec(attrString); // next match
        }
        result.push(currentTag);

        tagMatch = tagRe.exec(text);
    }
    return result;
}

parse('[data attr1="value1" attr2="value2" attr3="value3"]');
> [{name:'data',attrs:{attr1:'value1',attr2:'value2',attr3:'value3'}}]

```

This works for any number of tags in the string. The name of the tag does not matter.