Find Characters with Lazy Matching --- help

Find Characters with Lazy Matching --- help
0

#1

Tell us what’s happening:
Why is there an asterix in the code used in the explanation /t[a-z]*i/ ?

Doesn’t astrix mean that the immediate preceding condition is optional? In which case it should have matched and returned [“ti”] the first time without the need for a “?”.

Where lies my misunderstanding?

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/68.0.3440.106 Safari/537.36.

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


#2

The astrix means zero or more. I do not see a ? used in your regular expression.


#3

I’m talking about the explanation section, the titanic example.


#4

Now I understand your question. Without the ? after the , the [a-z] will keep grabbing consecutive letters a-z until there are no more. So it will grab the “i”, “t”, “a”, “n” after the “t” because these are the “more” part of “zero or more”.


#5

But if the regex is /t[a-z]*i/ and so in “titanic” the ‘t’ and ‘i’ are right next to each other, so then shouldn’t the regex be like, “matched ‘t’ now the optional [a-z] isn’t there and then there is ‘i’ so task complete”?


#6

The *? does not make the [a-z] optional. It just makes it not greedy, so it stops after the first match of [a-z]*, which is the “i” after the “t” in “titanic”.


#7

I’m so sorry I mistyped…I meant /t[a-z]*i/ which was my original question. I edited the earlier comment so you if you can please re-read my earlier reply and respond accordingly.