Overview
Validate form input such as colour codes with a short pattern, using character classes, counts and anchors.
- /pattern/.test(text) returns true or false
- Character classes [0-9a-f] and counts {6}
- Anchors ^ and $
- The i flag ignores case
Short Course
A regular expression describes a text shape; .test() says if it matches.
[0-9] is one digit, {3} repeats it. Trap: without ^ (start) and $
(end), the pattern may match just part of the text.
/[0-9]{3}/.test("ab1234"); // true — found inside
/^[0-9]{3}$/.test("ab1234"); // false
/^[0-9]{3}$/.test("123"); // true
/^[a-z]+$/i.test("Hello"); // true — i ignores case
Task
Write isHexColor(text) that returns true for # followed by exactly six
hex digits (0-9, a-f, any case), for example isHexColor("#1a2B3c") is
true and isHexColor("#12345") is false.
Test Suites
Pending
Next
Output