-2

i'm working on a tag parser to create a sort of html-like markup. I'm using RegExp to achieve this, as it deals with strings being sent through the server. The issue i'm having with this is utilizing multiple instances of the same tag in a line. Matching like this:

 "<item>a</item><item>b</item>".match(/\<item\>(.*)(\<\/\>|\<\/item\>)/gi)

The issue here is that the string i executed the match on returns as one match, I need each

 <item>...</item> or <item>...</> 

instance to be in a separate match. How can I make this happen?

  • 3
    obligatory: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – epascarello Mar 03 '17 at 02:38
  • 1
    Try `.*?` instead of `.*`. Anyway, why not use an existing XML parser? – Sebastian Simon Mar 03 '17 at 02:38
  • Kind of an aside: why are you escaping the `<` and `>` characters? – nnnnnn Mar 03 '17 at 02:41
  • Because it isn't xml, because it isn't html, and because I don't know any better. If not assumed, I'm not that great with RegExp (yet). – Tyler James Mar 03 '17 at 03:49
  • you can do xml and html with RegExp just fine, _EXCEPT_ when it has nested repetitive tag names. ex `
    `, and it sounds like that limitiation may be an issue for your syntax as well.
    – dandavis Mar 03 '17 at 04:12

1 Answers1

1

Use lazy quantifier (.*?) to match tags separately

<item>(.*?)(<\/>|</item>)

JavaScript

var tag = '<item>a</item><item>b</item><item>c</>';
var result = tag.match(/<item>(.*?)(<\/>|<\/item>)/ig);
console.log(result);
m87
  • 4,445
  • 3
  • 16
  • 31