Skip to content

Instantly share code, notes, and snippets.

@eouw0o83hf
Created December 28, 2014 21:49
Show Gist options
  • Save eouw0o83hf/df1e6410e06b19d46e11 to your computer and use it in GitHub Desktop.
Save eouw0o83hf/df1e6410e06b19d46e11 to your computer and use it in GitHub Desktop.
Javascript Simple Regex XML Parse
function tagRegExp(tag) {
return new RegExp("<" + tag + ">((.|[\r\n])*?)<\/" + tag + ">", "gmi")
}
function getElements(data, tag) {
var groups = data.match(tagRegExp(tag));
var output = [];
for(var i = 0; i < groups.length; ++i) {
var matches = tagRegExp(tag).exec(groups[i]);
if(matches.length >= 2) {
output.push(matches[1]);
}
}
return output;
}
function getElement(data, tag) {
return getElements(data, tag)[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment