Problem with lazy matching in Regex

Here the Match function should return the HTML tag <h1> and not the text "<h1>Winter is coming</h1>"

I am not sure how to modify the line to get the desired result!

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

Your browser information:

Challenge: Find Characters with Lazy Matching

Link to the challenge: text

in the description of the challenge there is written how to do.

do you understand the difference between greedy matching and lazy matching?

I think I kinda understood it ?!
1.a greedy match finds the longest possible part of a string that fits the regex pattern and returns it as a match.
2.The alternative is called a lazy match, which finds the smallest possible part of the string that satisfies the regex pattern.

I don’t how to get just <h1>

the default is greedy matching, and it is finding the max number of characters between a < and a > (as the patter is /<.*>/)
so it is getting everything between the first < at the beginning of the string, to the last > at the end of the string, it is matching the whole <h1>Winter is coming</h1> because it is greedy

instead you want to get only the opening tag <h1>, to make the match lazy

1 Like

Thank you so much. I got it :slight_smile: