3

I have a regex like this in my json file

"body": {
   "content": "<div class=(?:\"|')content(?:\"|') [^>](.*?)</div>\\;content:\\1",
}

As of now its only match first content div.

Can someone tell me how to make it greedy?

PrivateUser
  • 4,474
  • 12
  • 61
  • 94
  • what html are you trying to match? – user428517 Aug 08 '13 at 19:36
  • Do you want it to match all content until the last ? – seanmk Aug 08 '13 at 19:36
  • 1
    If you want it to match up to to `` that matches with the openining `
    ` you're going to have to use something more powerful than regex. Regex can't count. If you're using Javascript in a browser you can just use the native DOM.
    – Paul Aug 08 '13 at 19:39
  • @seanmk yes. Also I want to match every div that has content class using the global (g) modifier – PrivateUser Aug 08 '13 at 19:44

1 Answers1

2

.*? is a non-greedy (or lazy) quantifier. To make it greedy just remove the ?:

"body": {
   "content": "<div class=(?:\"|')content(?:\"|') [^>](.*)</div>\\;content:\\1",
}

Of course, as has been said many times before, you shouldn't use regular expressions to parse html.

To use the global mode, simply specify it when you're creating your RegExp, either like this:

"body": {
   "content": /<div class=(?:"|')content(?:"|') [^>](.*)</div>\\;content:\\1/g,
}

Or like this:

"body": {
   "content": new RegExp("<div class=(?:\"|')content(?:\"|') [^>](.*)</div>\\;content:\\1", "g"),
}

Of course at this point, it's no longer pure Json. Really, I'd recommend specifying the flags elsewhere. For example, in whatever code you have which actually does the html processing.

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331