1

I have the following

EmployeeTable.prototype.xmlToArray = function(xml) {

    // Parse the XML string into a XMLDocument
    var xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');

    var nodes = xmlDoc.getElementsByTagName("employee");
    var employees = [];

    for (var i = 0; i < nodes.length; i++) {
        employee = nodes[i];
        var innerHTML = employee.innerHTML;
        var name = innerHTML.filterEmployeesXML("name");
        console.log(name);
    }

};

which calls a function

String.prototype.filterEmployeesXML = function(valToFind) {
    var regex = new RegExp(/<name>([^<]*)<\/name>/);
    var match = employee.innerHTML.match(regex);
    return match[1];
};

However, I want to be able to do this for more than just name, i.e. I want to be able to filterEmployeesXML("jobTitle"), etc. where valToFind is the value to be found in the XML collection.

I've tried to do this using basic string concatenation, though I haven't a clue when it comes to Regex, any pointers?

thefourtheye
  • 206,604
  • 43
  • 412
  • 459
Jack hardcastle
  • 2,471
  • 3
  • 17
  • 36

1 Answers1

2

You can use the String representation of the Regular Expression with RegExp constructor, like this

new RegExp("<" + valToFind + ">([^<]*)<\/" + valToFind + ">");
thefourtheye
  • 206,604
  • 43
  • 412
  • 459