Regular Expressions: Find Characters with Lazy Matching / I don't understqnd the result given when ? is added at the end of the Regexp

Hello everyone,

I’m at Regular Expressions: Find Characters with Lazy Matching. Found the solution to the problem, but while trying different configuration to make sure I really understood, I don’t why

let text = "<h1>Winter is coming</h1>";
let myRegex = /<h.*>?/;
let result = text.match(myRegex);

Logs in the console ‘< h1 >Winter is coming</ h1 >’. Indeed, the regexp here as I understand it is for the shortest (because of the ? at the end) string that:

  • starts with ‘<h’
  • then has a wildcard sign (or not actually since there’s the *)
  • and has ‘>’ at the end

I would have thought it would have printed ‘< h1 >’ since it’s the shortest string to match all conditions, but it printed the whole phrase and I don’t get why :thinking:

Hello there,

The ? (question mark) at the end just signifies zero or one > (gt). The ? can be used to do as you say match as few times as possible, but that only happens when it is preceded by what I call a non-definite quantifier.

Let me search the actual term…
EDIT: I cannot find a specific term for what the ? is called during lazy matching, but this is an example of a lazy quantifier: *?

Hope this helps

1 Like

Thx a lot @Sky020! It was not clear to me in lesson that ? signifies *zero or one and also it has to be used in conjonction with a * (or + I guess)

That was blazing fast help! I’m impressed and most thankful1

1 Like

on its own ? means “zero or one”, so a? zero or one a, colou?r , “0 or one u” (both “color” and “colour”)

instead in front of a quantifier makes it lazy:
string “banana”
/b.+n/ matches “banan”
/b.+?n matches “ban”

1 Like