Anchors — Regex

An anchor matches a position, not a character. ^ is the start of the text, $ the end, \b the boundary between a word character (\w) and a non-word one…

Mrs. Ratna's PO form asks for a phone number. Someone types my no. 07700900461, and the check says it's valid. The pattern 07\d{9} finds a number inside that input, and for the pattern that's enough.

What's missing: the pattern only says "what is in it", not "where". That is the job of an anchor. An anchor doesn't match a character, it matches a position: the start of the text, the end of the text, or a word boundary. Like the start line and the finish line on a running track: a line isn't a runner and takes up no track; it only marks a place. ^ is the start line, $ the finish line, and \b the line between words.

^ and $: start and end of the text

Anchors matter most for validating form input. Without anchors, test() is satisfied by a matching piece anywhere.

const validNumber = /^07\d{9}$/;

console.log(validNumber.test("07700900461"));        // true
console.log(validNumber.test("my no. 07700900461")); // false
console.log(/07\d{9}/.test("my no. 07700900461"));   // true
console.log(/^\d{5}$/.test("12345"), /^\d{5}$/.test("123456")); // true false

An anchor uses up no characters. /^[A-Z][a-z]*/ means "an uppercase letter in the first position, then lowercase letters": .test("Ratna") answers true, .test("dina") answers false.

^ has two meanings. Outside square brackets it is the start-of-text anchor. Right after [ it negates: [^0-9].

The m flag: every line

Our chat has 6 lines. With no flag, ^ and $ are only the start and end of the whole text. With the m flag (multiline), they apply at the start and end of every line.

console.log(chat.match(/PO-\d{4}$/g));     // ['PO-0413']
console.log(chat.match(/PO-\d{4}$/gm));    // ['PO-0412', 'PO-0413']
console.log(chat.match(/^\[12/g).length);  // 1
console.log(chat.match(/^\[12/gm).length); // 6

Without m, $ only matches at the end of the last line, so only PO-0413 is found. PO-0412 is at the end of the first line, and that is not the end of the text. With m, both are found, and ^\[12 finds the date at the start of all six lines.

\b: the word boundary

\b matches the position between a word character (\w: a letter, a digit, an underscore) and a non-word character, or the edge of the text.

console.log(chat.match(/\d{10}/g));      // ['1234567890', '0770090046']
console.log(chat.match(/\b\d{10}\b/g));  // ['1234567890']
console.log(chat.match(/tart/gi));       // ['Tart', 'tart', 'TART']
console.log(chat.match(/\btart\b/gi));   // ['Tart', 'TART']

⚠️ Why does wrong input still pass?

TL;DR: An anchor matches a position, not a character. ^ is the start of the text, $ the end, \b the boundary between a word character (\w) and a non-word one. Validate input with ^...$ so the whole text is checked: /^\d{5}$/.test("12345") answers true, /^\d{5}$/.test("123456") answers false. The m flag makes ^ and $ apply on every line. \b separates tart from tarts and the bank account from a piece of a phone number, but not PO from 0412 in PO0412. Don't use the g flag on a regex for test(). ^ outside square brackets means start; right after [ it means negation.

⚡ An anchor is a position, not a character

Write:

const validNumber = /^07\d{9}$/;
console.log(validNumber.test("my no. 07700900461"));
Guess first: there is a valid number inside it. Is the result true or false?

false. ^ asks the match to begin at the start of the text and $ asks it to end exactly at the end, so the whole field has to be a number. Without the anchors, the piece 07700900461 inside it is enough for true.

Meaning: ^ is the start of the text, $ the end. /^[A-Z][a-z]*/ means an uppercase letter in the first position: .test("Ratna") answers true, .test("dina") answers false.

⚡ The m flag: every line

Write:

console.log(chat.match(/PO-\d{4}$/gm)); // ['PO-0412', 'PO-0413']

Meaning: Without m, $ is only the end of the whole text, so only PO-0413 is found. With m, ^ and $ hold at the start and end of every line.

⚡ \b: a word boundary

Write:

console.log(chat.match(/\btart\b/gi)); // ['Tart', 'TART']

Meaning: \b is the position between a word character (\w) and a non-word one. tarts is turned down, because after tart there is still the letter s.

What you will learn