Frage

g -- This option finds all occurrences of the pattern in the string

Does that mean that it is greedy? Because it makes the regex engine parse the entire string and not return at the first match. That is what non greedy matching is meant right. Can it be even classified as greedy or non greedy?

War es hilfreich?

Lösung 2

Greedy will consume as much as possible.

So for example <.+> will match whole <EM>first</EM>

However <.+?> will match only <EM> as it is lazy matching.

Now you must understand that /g modifier can be used with both greedy matching and lazy matching.

For example,

<EM>first</EM>

and you use <.+>/g it will replace full expression with specified one.

However <.+?>/g will replace <EM> and </EM> only with specified expression.

Andere Tipps

g is the global flag. It means that it will keep looking for matches after it finds one.

g is not greedy. The term greedy in regex is used to indicate that a token will match as many characters as possible, giving back as needed. Eg. \w+

A possessive match indicates that a token will match as many characters as possible, without giving back. Eg. \w++

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top