Why does .*? in the regex match h1?

Tell us what’s happening:
Why does .*? in the regex match h1? I understand it as: Any character can appear 0 or more times (the dot and the star) and with lazy matching (the question mark), the shortest substring of that I would say is either the empty string or h. But not h1, but the code below passes the challenge. Can someone be so kind to enlighten me?

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Find Characters with Lazy Matching

Link to the challenge:

It works because the .*? in your regex is enclosed in <>, meaning that regex can only find something that starts with < and ends with >, that’s why it doesn’t just find an empty string or h, because those don’t start with < and don’t end with >. So the regex matches <h1>

2 Likes

Ahh ofc, I didnt think of that, thank you so much for explaining it! :slight_smile:

1 Like