RegexpGarden










- Garden Text Representation
12345
let garden = [
"Rose", "Primrose",
"Rose", "Rosemary",
"Rose"
];
- Code Editor
- Log
123456
garden = garden
.map(plant =>
plant.replace(//i, "Blackberry")
)
);
Lesson task •
Replace all the roses with blackberries
Replace part of string
String.prototype.replace
One of the most powerful features of regular expressions is that with regular expressions you can not only check if the substring is present in the string, but also replace a substring matching pattern with another string provided:
1
console.log("foo bar".replace(/foo/i, "baz")) // baz bar
By default only first match will be replaced:
1
console.log("foo bar foo".replace(/foo/i, "baz")) // baz bar foo
But later we'll see that there is a special flag that alters this behaviour