Challenge: Find Characters with Lazy Matching

Hi guys,could someone explain me the solutions of this problem?I´m new to programing and can´t understand it.

Your code so far


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

Your browser information:

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

Challenge: Find Characters with Lazy Matching

Link to the challenge:

We don’t explain problems as you require. First, tell us what it is that you don’t understand, and what you’ve tried so far.

As a first hint, regex means regular expression. Which is an instruction for the engine to find a particular string i.e a word, set of words, number, symbol.

A real life example would be this: find all people with red jackets and blue buttons in a disco. In regex, you tell the computer this, and it looks for it.

In the code you pasted there are three variables. The regex one is what I’ve explained before. The result variable will store the result of your search (regex) over text.

1 Like

i didn´t understand why it didn´t work without the question mark ("?") but it works with it, could you explain me the role of the question mark?

@marcelo12.renato marcelo12.renato

Yes.

If we use <.*> it will match this expression <h1>lnasfnafb'lnadf</h1> as the code means anything between < and >. It is the longest match.

We want the lazy match, so we get only and solely the short expression. Then it matches <h1> and stops searching.

Edit: you can console.log(result) to test it yourself.

1 Like

thanks,have a good day

1 Like