JavaScript, inside out · Chapter 35 of 51
Regular expressions
By the end of this chapter you can describe the shape of a piece of text in one pattern, and use it to check, find, pull apart and rewrite text. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modern JavaScript
- 6 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Delivery is live, and the sign-up form is a mess. Customers type phone numbers as 98450 12345, +91-9845012345 and call me. UPI IDs arrive as ravi@ and ravi@@ybl, and a rider drove to Indiranagar with the PIN code 56003. Anjali wants the form to catch these, Ravi wants the order lines in kitchen notes read into numbers, and receipts must hide phone numbers.
With what you know so far, checking a phone number means a pile of string methods: startsWith('+91'), slice, a loop over the characters, a length check. Every format needs its own branch, and the one nobody thought of slips through.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
isValidPhone: Indian mobiles, with or without +91
Write isValidPhone(str). Return true when the whole string is an Indian mobile number: 10 digits, the first of them 6, 7, 8 or 9. Before them there may be '+91' (followed by at most one space or one hyphen) or a single '0', and nothing else.
Valid: '9845012345', '+91 9845012345', '+91-9845012345', '+919845012345', '09845012345'. Invalid: '5845012345', 9 or 11 digits, '98450 12345', 'call 9845012345', '9845012345 now', '+92 9845012345', '+91 9845012345' (two spaces), '009845012345' and ''.
- Task 2
isValidUpi: a UPI ID shaped like handle@bank
Write isValidUpi(str). Return true when the whole string is a UPI ID: a handle of at least 2 characters, each a letter, digit, dot, hyphen or underscore; then exactly one '@'; then a bank name of at least 2 letters and nothing else (no digits, no spaces). Letters may be upper or lower case.
Valid: 'meera.tapri@okaxis', 'ravi_99@ybl', '9845012345@paytm', 'anjali-k@OKHDFCBANK', 'kb@upi'. Invalid: 'ravi@', '@ybl', 'ravi@@ybl', 'ravi@ok axis', 'ravi@123', 'r@ybl', 'ravi ybl', 'ravi kumar@ybl', 'ravi@ybl@sbi', 'ravi@y'.
- Task 3
extractPins: every PIN code in a delivery note
Write extractPins(text). Return an array of every PIN code in the text, as strings, in the order they appear. A PIN code is exactly six digits, the first one not 0, standing on its own: not part of a longer number.
'Deliver to 560038, then 560001' gives ['560038', '560001']. In 'Not 0560038, not 5600381, not 56003, but 400001.' only '400001' counts. A text with no PIN codes gives [], not null.
- Task 4
parseOrders: order lines read with named groups
Write parseOrders(text). The text has one entry per line. Read every line shaped like '<qty> x <item> @ <price>' and skip every other line. The space before x is optional and at least one space must follow it; spaces around @ are optional.
Return an array of objects { qty, item, price }, in the order of the lines, with qty and price as numbers and item as text without the spaces around it. '2 x masala chai @ 40' gives { qty: 2, item: 'masala chai', price: 40 }, '3x cutting chai @25' gives { qty: 3, item: 'cutting chai', price: 25 }, and a text with no order lines gives [].
- Task 5
maskPhones: hide phone numbers on receipts
Write maskPhones(text). Return the text with every Indian mobile number in it replaced by 'XXXXXX' followed by its last four digits. A mobile here is exactly 10 digits starting with 6, 7, 8 or 9, standing on its own.
'Call 9845012345 or 7012345678, PIN 560038.' gives 'Call XXXXXX2345 or XXXXXX5678, PIN 560038.' A longer number such as 98450123456789, a number starting with 5, and a text with no numbers stay exactly as they are.
- Task 6 · interview question
Interview: checkPins, fix the PIN checker
Fix checkPins(pins). It returns an array with true or false for each PIN code, in order: true when the code is exactly six digits and does not start with 0.
It is broken now: valid codes are sometimes rejected, so checking ['560038', '560001', '400001', '110001'] does not give four trues. The same PIN must get the same answer every time: ['560038', '012345', '560038', '56003'] gives [true, false, true, false].
What you will learn
- Patterns: character classes, counts, anchors and flags.
- test, match and replace: check, find every match, and rewrite with what you found.
- Named groups and matchAll: pull the parts out of every match.
- The g flag's memory: why one regex can answer differently on the same text.
More in Modern JavaScript
Build "Regular expressions" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.