Back to Freecodecamp

Find Characters with Lazy Matching

curriculum/challenges/english/blocks/regular-expressions/587d7db6367417b2b2512b9b.md

latest1.7 KB
Original Source

--description--

In regular expressions, a <dfn>greedy</dfn> match finds the longest possible part of a string that fits the regex pattern and returns it as a match. The alternative is called a <dfn>lazy</dfn> match, which finds the smallest possible part of the string that satisfies the regex pattern.

You can apply the regex /t[a-z]*i/ to the string "titanic". This regex is basically a pattern that starts with t, ends with i, and has some letters in between.

Regular expressions are by default greedy, so the match would return ["titani"]. It finds the largest sub-string possible to fit the pattern.

However, you can use the ? character to change it to lazy matching. "titanic" matched against the adjusted regex of /t[a-z]*?i/ returns ["ti"].

Note: Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.

--instructions--

Fix the regex /<.*>/ to return the HTML tag <h1> and not the text "<h1>Winter is coming</h1>". Remember the wildcard . in a regular expression matches any character.

--hints--

The result variable should be an array with <h1> in it

js
assert(result[0] == '<h1>');

myRegex should use lazy matching

js
assert(/[^\\][\*\+\?]\?/.test(myRegex));

myRegex should not include the string h1

js
assert(!myRegex.source.match('h1'));

--seed--

--seed-contents--

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

--solutions--

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