All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Regular expressions for validation

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

A regular expression (regex) is a pattern that text is tested against. It is used to check formats such as postcodes, codes and phone numbers (spec 9.2.1 to 9.2.3). Exam papers regularly ask you to read and write them.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Writing and testing a pattern

const pattern = /^[A-Z]{2}[0-9]{3}$/;
console.log(pattern.test("AB123"));
console.log(pattern.test("ab123"));
console.log(pattern.test("AB1234"));

Output:

true
false
false

In JavaScript a regex is written between forward slashes. test(string) returns true if the string matches. ^ means the start of the string and $ the end: without them the pattern only has to appear somewhere inside the string, so AB1234 would pass because it contains AB123.

Matching characters (9.2.2)

PatternMatches
`[A-Z]`One upper-case letter
`[a-z]`One lower-case letter
`[A-Za-z]`One letter of either case
`[0-9]` or `\d`One digit
`[A-D]`One of A, B, C or D only
`[0-9]`Any character that is **not** a digit
`\w`A letter, digit or underscore
`\s`A space, tab or new line
`.`Any character except a new line
`\.`, `\?`, `\+`A literal full stop, question mark or plus sign (\ escapes a special character). A hyphen outside square brackets needs no escape
`[!@#?]`One of these punctuation symbols
`(cat|dog)`cat or dog

Repeating characters and digits (9.2.3)

QuantifierMeaningExample
`{3}`Exactly 3 times`[0-9]{3}` is three digits
`{2,4}`From 2 to 4 times`[A-Z]{2,4}`
`{8,}`8 or more times`.{8,}` is at least 8 characters
`+`1 or more times`[a-z]+`
`*`0 or more times`[0-9]*`
`?`0 or 1 time (optional)`colou?r` matches color and colour

Building a pattern step by step

A UK-style postcode such as LS1 4AP or SW19 5HX: one or two letters, one or two digits, an optional space, a digit, two letters.

const postcode = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/;
const tests = ["LS1 4AP", "SW19 5HX", "LS14AP", "L1 4A", "ls1 4ap"];
for (const t of tests) {
  console.log(t + ": " + postcode.test(t));
}

Output:

LS1 4AP: true
SW19 5HX: true
LS14AP: true
L1 4A: false
ls1 4ap: false

Adding i after the closing slash (/.../i) makes the pattern ignore case. Real postcodes have more rules (some have a letter after the digits, such as EC1A 1BB, which this pattern rejects), so state what your pattern accepts. The same pattern without the slashes can go in an HTML pattern attribute.

Worked example

Write a regular expression for a membership code: the letters RSC, a hyphen, then exactly four digits (for example RSC-0457).

  1. Start anchor ^, then the literal letters RSC.
  2. A hyphen outside square brackets is an ordinary character, so just type -. (Writing \- works in a JavaScript regex literal but makes the pattern invalid in an HTML pattern attribute, which browsers then ignore.)
  3. Exactly four digits: [0-9]{4}, then the end anchor $.

Answer: `/RSC-[0-9]{4}$/`, which accepts RSC-0457 and rejects RSC-457.

Key idea: A regex is a pattern between slashes; test() returns true or false. ^ and $ anchor the whole string. [A-Z], [a-z], [0-9] (or \d) match one character; {n}, {n,m}, +, * and ? repeat; escape special characters with \.

Check yourself

  1. Which of these strings does the pattern match? ```js /^[0-9]{4}$/ ```
    • `20265`
    • `abcd`
    • `2026`
    • `202`
    Show answer

    `2026`. ^ and $ make the whole string exactly four digits.

  2. Which of these strings does the pattern match? ```js /^[A-Z][a-z]+$/ ```
    • `Amira`
    • `amira`
    • `AMIRA`
    • `A`
    Show answer

    `Amira`. One capital, then one or more lower-case letters. 'A' fails because + needs at least one lower-case letter.

  3. What is displayed when this code runs? ```js const re = /^[A-Z]{3}[0-9]{2}$/; console.log(re.test("ABC12"), re.test("AB123"), re.test("abc12")); ```
    Show answer

    true false false. Only ABC12 has three capitals then two digits: true false false.

  4. A regex should accept any single upper-case letter from A to Z. A candidate wrote [A-D]. Why is it wrong?
    • It accepts lower-case letters too
    • It only accepts A, B, C and D
    • It accepts digits
    • Square brackets are not allowed in regular expressions
    Show answer

    It only accepts A, B, C and D. Pearson's 2023 report noted candidates using [A-D] instead of the full range [A-Z].

  5. Which of these strings does the pattern match? ```js /colou?r$/ ```
    • `colouur`
    • `colr`
    • `colours`
    • `color`
    Show answer

    `color`. u? makes the u optional: color and colour match; colouur has two u's and colours has an extra s.

  6. Write a regular expression, including the slashes, that matches a string made of exactly six digits and nothing else.
    Show answer

    /^[0-9]{6}$/. Anchors ^ and $, the digit class and the quantifier {6}.

  7. Which of these strings does the pattern match? ```js /07[0-9]{9}$/ ```
    • `0712345678`
    • `7123456789`
    • `071234567890`
    • `07123456789`
    Show answer

    `07123456789`. 07 and then exactly nine more digits: 11 digits in total.

  8. Which strings does `/[0-9]{3}/` (without ^ and $) match?
    • Only strings that are exactly three digits
    • Only strings with no digits
    • Any string that contains three digits in a row somewhere, such as AB1234
    • Only strings that start with three digits
    Show answer

    Any string that contains three digits in a row somewhere, such as AB1234. Without anchors the pattern can match part of the string.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.