ProgramGuru

Match a Simple Text Pattern

Match a Simple Text Pattern

A regular expression describes a text pattern. Anchors require the whole roll-number label to fit, rather than accepting a matching substring inside unrelated text.

Before this topic, review Represent Dates and Time Explicitly if its terms are unfamiliar.

What you’ll learn

  • Explain match a simple text pattern through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: The first label now has only two digits, so neither input matches the anchored three-digit pattern.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

const rollPattern = /^R-[0-9]{3}$/;
console.log(rollPattern.test("R-072"));
console.log(rollPattern.test("prefix R-072"));

Expected output

true
false
Run in JavaScript compiler →

Recognize the important boundary

A pattern is not a complete parser or validation policy. Global and sticky expressions carry lastIndex state across some calls.

Try a small variation

JavaScript · original source for practice

const rollPattern = /^R-[0-9]{3}$/;
console.log(rollPattern.test("R-072"));
console.log(rollPattern.test("prefix R-072"));

Your task: Replace 'R-072' with 'R-72'. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

The first label now has only two digits, so neither input matches the anchored three-digit pattern.

JavaScript · complete worked variation

const rollPattern = /^R-[0-9]{3}$/;
console.log(rollPattern.test("R-72"));
console.log(rollPattern.test("prefix R-072"));

Expected output

false
false
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const rollPattern = /^R-[0-9]{3}$/;
console.log(rollPattern.test("R-072"));
console.log(rollPattern.test("prefix R-072"));
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

true → false

A regular expression describes a text pattern. Anchors require the whole roll-number label to fit, rather than accepting a matching substring inside unrelated text. A pattern is not a complete parser or validation policy. Global and sticky expressions carry lastIndex state across some calls.

Keep the useful idea

A regular expression describes a text pattern. Anchors require the whole roll-number label to fit, rather than accepting a matching substring inside unrelated text.

Check your variation against this explanation: The first label now has only two digits, so neither input matches the anchored three-digit pattern.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Pass an Operation to Another Function →