Find Characters with Lazy Matching

please help me :relieved:

Your code so far


let text = "<h1>Winter is coming</h1>";
let myRegex = /<h1>[a-z]*?i/; // 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/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/find-characters-with-lazy-matching

@arrbxr This sentence in the challenge is important:

Fix the regex /<.*>/ to return the HTML…

Generally, when it says fix it means one only needs to make a small change. Does that help?

idk about him but it helped me a lot :slight_smile: Thanks!

I was confused about this but this solved /<h1.*?>/

Try this if you’re stuck

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

witch is the best answer to this question?

This is my solution:
let myRegex = /<h1*?>/;

Basically you’re finding the shortest route from “<h1” to the next “>”. Without the question mark, it would find the longest route to the next “>” which would return the whole variable.

Can someone confirm my explanation/answer?

I think FCC challenges are confusing sometimes.
My solution was <h1>
not even putting anything else.

Reading “Remember the wildcard . in a regular expression matches any character.”
I think the correct solution should keep the wildcard . so I tried with myRegex = /<.*?>/;
and pass the test
“Great is the confusion under the sky. The situation is excellent” :slight_smile:

2 Likes

This worked. thank you bud :+1:

The most efficient way to match this is /<.*?>/ or /<.+?>/ although this will work but it will only match only opening tag <h1> and not </h1> accompany it with g to get all the matches.