最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript regex for xmlhtml attributes - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 4

In 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)
发布评论

评论列表(0)

  1. 暂无评论