A pot
Rosemary
A pot
RoseIceberg
A pot
Primrose
A pot
RoseDoubleDelight
A pot
Rose
A pot
RoseQueenElizabeth
1234
let garden = "Rosemary\tRose 'Iceberg'\t
Primrose\t
Rose 'Double Delight'\tRose\t
Rose 'Queen Elizabeth'";
123456
garden = garden
.replace(//gi, "$1");
Lesson 41 / 42

Lesson task •

Remove all the cultivars in the plants names

Laziness

By default * and + are eager: once they find occurrence they try to capture as much as they can.

123
console.log("foo 42 something 42".replace(/foo.*42/gi, "bar")) // bar 
console.log("foo 42 other 42".replace(/foo.*42/gi, "bar")) // bar 
console.log("foo 42 42".replace(/foo.*42/gi, "bar")) // bar 

In this example you can see that .* match doesn'stop on the first encountering of the "42" although it would be a valid match: it expanded till the last "42". To alter this behavior, we should follow it with ?:

123
console.log("foo 42 something 42".replace(/foo.*?42/gi, "bar")) // bar something 42 
console.log("foo 42 other 42".replace(/foo.*?42/gi, "bar")) // bar other 42 
console.log("foo 42 42".replace(/foo.*?42/gi, "bar")) // bar 42