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
^at the start of the pattern: the match has to begin at the start of the text.$at the end of the pattern: it has to finish exactly at the end of the text.- With both, the whole input has to match.
my no. 07700900461is rejected because there is text before07. - Without anchors, the same pattern accepts that input, because the piece
07700900461inside it matches. /^\d{5}$/only accepts exactly 5 digits:12345passes,123456(6 digits) doesn't.
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']
\d{10}also cuts the first 10 digits out of Sari's number07700900461. With\bon both sides, the 10 digits have to stand alone, which leaves only the bank account1234567890.\btart\bturns downtarts, because aftertartthere is still the letters: no boundary there.- Digits are word characters too. In
PO0412(an order code typed without its dash) there is no boundary betweenOand0, so/\b0412\b/.test("PO0412")answersfalse. The same goes for3in3jars.
⚠️ Why does wrong input still pass?
- Forgetting anchors when validating.
/07\d{9}/.test("my no. 07700900461")answerstrue. Validating input always uses^...$. ^and$on multi-line text withoutm. Only the first line (for^) and the last line (for$) are checked.- The
gflag on a regex used fortest(). A regex withgremembers where it stopped inlastIndex.const re = /^07\d{9}$/g, thenre.test()on three numbers that are all valid, givestrue,false,true: the second test starts looking at position 11, fails, andlastIndexgoes back to 0. For validation, leavegoff. \band digits.\bsees no boundary between a letter and a digit, because both are word characters.
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.