JavaScript · Level 2 Text

Regular Expressions

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

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

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.

solution.js
Test Suites Pending
Output