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);
console.log(result);
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
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: *?