Literal matching and .test()
A regex in JavaScript is written between slashes: /cat/ is a pattern, not a
string. Most characters inside it are literals - they match themselves,
letter for letter. Once you have a pattern, pattern.test(str) asks the
simplest possible question: does this shape appear anywhere in str? It
returns true or false - nothing else.
By default a match can happen anywhere in the string, not just at the start,
and it's case-sensitive - /cat/ matches inside "concatenate" just as
happily as inside "a cat sat", but never matches "CAT".
Your task: write hasCat(str), returning whether str contains the
literal text "cat" anywhere, using a regex and .test().
You'll practice:
- Writing a regex literal with
/.../
- Checking a match with
.test()
- Regex matches are case-sensitive and match anywhere by default
Related reading: What a Regex Actually Is →