PrivateUser
PrivateUser

Reputation: 4524

How to make this json regex greedy

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?

Upvotes: 3

Views: 236

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

.*? 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.

Upvotes: 2

Related Questions