Regex Patterns
A time on a clock always looks the same: two digits, a colon, two digits, like 09:45. You can see at once that 9:4 or 09:450 isn't a time, even if you have never seen those numbers before. You know its shape. A regex can describe a shape too.
\d: any digit
\d matches one digit, any of 0 to 9:
console.log(/\d/.test("Room 7"));
console.log(/\d/.test("Room seven"));
true
false
[ ]: one of these
Square brackets match one letter from a list. [abc] matches an a, a b or a c. A dash gives a range: [A-Z] is any capital letter, [a-z] is any small letter, and [0-9] is any digit, like \d:
console.log(/[A-Z]/.test("hello"));
console.log(/[A-Z]/.test("hello Sofia"));
false
true
{ }: how many
A number in curly brackets says how many times the thing before it comes, one right after the other. \d{4} is four digits:
console.log(/\d{4}/.test("Born in 2011"));
console.log(/\d{4}/.test("Room 12"));
true
false
^ and $: the whole text
Until now, a pattern looks for a match anywhere in the text. That often lets too much in:
console.log(/\d{2}:\d{2}/.test("09:45"));
console.log(/\d{2}:\d{2}/.test("109:450"));
true
true
109:450 holds 09:45 inside it, so it matches. ^ means "the start of the text", and $ means "the end of the text". Between them, the pattern must be the whole text:
console.log(/^\d{2}:\d{2}$/.test("09:45"));
console.log(/^\d{2}:\d{2}$/.test("109:450"));
true
false
In a page
Lyon's postal codes are five digits, like 69001:
<label for="postal">Postal code</label>
<input id="postal" />
<p id="shape">Five digits, please</p>
<script>
const postal = document.querySelector("#postal");
const shape = document.querySelector("#shape");
postal.addEventListener("input", () => {
if (/^\d{5}$/.test(postal.value)) {
shape.textContent = "Looks like a postal code";
} else {
shape.textContent = "Five digits, please";
}
});
</script>
Type 6900: Five digits, please. Type one more digit: Looks like a postal code. A sixth digit, or a letter anywhere, and it says Five digits, please again.
Break it on purpose
Remove the ^ and the $:
if (/\d{5}/.test(postal.value)) {
Type 690012345: the page says Looks like a postal code. There are five digits in a row inside it, and nothing says the text must stop there. When a pattern checks a whole answer, it starts with ^ and ends with $.
Mission: Your badge
What you will see: under the email box, a new box asks for the badge number. When the page opens, the pass says Badge: No valid badge yet. Type OS4821, and it says Badge: OS4821. Type OS482: No valid badge yet again.
Everybody on the station wears a badge, and every badge has a number: two capital letters and four digits, like OS4821. The station wants it on the booking.
- In the form, right after the email box, add a box with the id
badge-numberand the namebadge, with its label. - On the pass, an element with the id
pass-badgeshows the number while it's typed, when it is exactly two capital letters and four digits. - If not, and while the box is empty, it says
No valid badge yet. os4821,OS48215andXOS4821aren't badge numbers.