1

Afternoon all,

Im trying to find all xml tags that contain a particular word - ie using a wildcard.

The xml is used as such:

xml = $('<elements>' + $('#form_template').val() + '</elements>');

Where in #form_template contains the xml contents. I then want to search the entire xml tree for tagnames starting with the word item. I've tried xml.match(/item_[0-9]+/) but this returns match is not a function.

Any clue how to search for tags that contain item throughut the xml?

Regards,

EDIT: Code sample This is used to create a zend form. The html would be irrelevant for the example.

<QUESTION1>
<type>MultiCheckbox</type>
<options>
<class>rulem1 type-1 standard</class>
<label>Fabs and Equipments</label>
<multioptions>
<item-separate_equipments>Separate Equipments</item-separate_equipments>
<item-lasers>Lasers</item-lasers>
<item-metrology>Metrology / Inspection</item-metrology>
<item-turnkey>Turnkey Lines</item-turnkey>
<item-factory>Factory Design</item-factory>
</multioptions>
</options>
</QUESTION1>
<QUESTION2>
<type>MultiCheckbox</type>
<options>
<class>rulem1 type-1 standard</class>
<label>Material and Components</label>
<multioptions>
<item-backsheets>Backsheets</item-backsheets>
<item-raw_materials>Raw Materials</item-raw_materials>
<item-auxiliary>Auxiliary Materials</item-auxiliary>
<item-cell_coating>Coating</item-cell_coating>
<item-pastes>Pastes / Metallisation</item-pastes>
<item-tf>TF Gas</item-tf>
<item-foils>Foils</item-foils>
<item-encapsulation>Encapsulation</item-encapsulation>
<item-aluminium>Aluminium</item-aluminium>
<item-junction>Junction Box and Connection Systems</item-junction>
<item-materials_others>Others</item-materials_others>
</multioptions>
</options>
</QUESTION2>
mr12086
  • 1,137
  • 13
  • 29
  • Can you provide the relevant html and a small snippet of the XML? – iambriansreed May 16 '12 at 15:12
  • Don't use regex to parse XML. Get an XML parser. I'm pretty certain javascript/jQuery can do this. [link](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Rohaq May 16 '12 at 15:22

1 Answers1

3

You should be able to use .find(*) to recursively find all tags and then filter them yourself, e.g.:

var res = xml.find('*').filter(function() {
    return this.nodeName.match(/item_[0-9]+/i);
});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309