RegexpGarden












- Garden Text Representation
1234
let garden = [
"Rose", "Primrose", "Rice", "Roscoea",
"Rosemary", "Christmas Rose"
];
- Code Editor
- Log
123456
for (const plant of garden) {
if (plant.match(//i) {
water(plant);
}
}
Lesson task •
Water all the plants, starting with "rose"
Start of string
^
Points to the start of string. The pattern after it will be applied only to the beginning of the string, but not to the other partsBy default, a regular expression looks for a substring matching the pattern at any place of the string. You can restrict the search to the substrings that includes the starting of the string by using ^:
12
console.log(!!"foo bar".match(/^foo/)) // true
console.log(!!"bar foo".match(/^foo/)) // false
If you need to match "^" literally than you have to escape it with backslash:
12345
console.log(!!"^foo".match(/\^foo/)) // true
console.log(!!"^foo".match(/^foo/)) // false
console.log(!!"^foo".match(/^\^foo/)) // true
console.log(!!"foo^".match(/foo\^/)) // true
console.log(!!"foo^".match(/foo^/)) // false