RegexpGarden












- Garden Text Representation
1234
let garden = "Rosemary\tRose 'Iceberg'\t
Primrose\t
Rose 'Double Delight'\tRose\t
Rose 'Queen Elizabeth'";
- Code Editor
- Log
123456
garden = garden
.replace(//gi, "$1");
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