









- Garden Text Representation
let garden = [
"Rue", "Rose", "Rye",
"Rosemary",
"Rice"
];
- Code Editor
- Log
for (const plant of garden) {
if (plant.match(//) {
water(plant);
}
}
Lesson task •
Water plants, starting with "Rose"
Match literally
Regular Expressions (shortly - regexp, or regex) is a special language to lookup for a pattern in a text and to replace it with another pattern. Regexps are present in many programming languages and its implementations are very similar although sometimes has its unique flavor. In this course we will learn how to write regexps in JavaScript. But it's not much differs from other languages regexp implementations, so after finishing this course you'll be able to write regular expressions in every language.
Regexps follows synhtax: /PATTERN/parameters flags(optional). Flags allow to configure options like case sensitivity. In this lesson for a sake of simplicity we use regexps without flags.
By default, string matches a regular expression if it includes a substring matching a pattern specified in a regular expression at any place. A simplest pattern is a literal match:
console.log(!!"foo bar".match(/foo/)) // true
console.log(!!"foo bar".match(/baz/)) // false
console.log(!!"foo bar".match(/bar/)) // true
console.log("foo bar".replace(/foo/, "baz")) // baz bar
console.log(!!"Foo Bar".match(/foo/)) // false