admin管理员组文章数量:1319006
I cant seem to be able to build a good regex expression (in javascript) that extracts each attribute from an xml node. For example,
<Node attribute="one" attribute2="two" n="nth"></node>
I need an express to give me an array of
['attribute="one"', 'attribute2="two"' ,'n="nth"']
... Any help would be appreciated. Thank you
I cant seem to be able to build a good regex expression (in javascript) that extracts each attribute from an xml node. For example,
<Node attribute="one" attribute2="two" n="nth"></node>
I need an express to give me an array of
['attribute="one"', 'attribute2="two"' ,'n="nth"']
... Any help would be appreciated. Thank you
Share Improve this question asked Jul 25, 2011 at 2:39 JamesJames 31 silver badge2 bronze badges 4- 4 Time for the obligatory link. – Kerrek SB Commented Jul 25, 2011 at 2:46
- Why wouldn't you just use an XML parser library? – jfriend00 Commented Jul 25, 2011 at 2:54
- 1 @jfriend00 - probably because browsers have a built–in XML parser and suitable DOM methods already. – RobG Commented Jul 25, 2011 at 3:13
- I'm not sure i want the overhead of an xml parser library, plus i'm rarely ever going to have well formed xml. im actual parsing the diff generated by git. – James Commented Jul 26, 2011 at 1:30
4 Answers
Reset to default 4In case you missed Kerrek's ment:
you can't parse XML with a regular expression.
And the link: RegEx match open tags except XHTML self-contained tags
You can get the attributes of a node by iterating over its attributes property:
function getAttributes(el) {
var r = [];
var a, atts = el.attributes;
for (var i=0, iLen=atts.length; i<iLen; i++) {
a = atts[i];
r.push(a.name + ': ' + a.value);
}
alert(r.join('\n'));
}
Of course you probably want to do somethig other than just put them in an alert.
Here is an article on MDN that includes links to relevant standards:
https://developer.mozilla/En/DOM/Node.attributes
try this~
<script type="text/javascript">
var myregexp = /<node((\s+\w+=\"[^\"]+\")+)><\/node>/im;
var match = myregexp.exec("<Node attribute=\"one\" attribute2=\"two\" n=\"nth\"></node>");
if (match != null) {
result = match[1].trim();
var arrayAttrs = result.split(/\s+/);
alert(arrayAttrs);}
</script>
I think you could get it using the following. You would want the second and third matching group.
<[\w\d\-_]+\s+(([\w\d\-_]+)="(.*?)")*>
The regex is /\w+=".+"/g
(note the g
of global).
You might try it right now on your firebug / chrome console by doing:
var matches = '<Node attribute="one" attribute2="two" n="nth"></node>'.match(/\w+="\w+"/g)
本文标签: javascript regex for xmlhtml attributesStack Overflow
版权声明:本文标题:javascript regex for xmlhtml attributes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742053253a2418161.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论