πŸ‘¨β€πŸŽ“A Quick Start Guide to RegEx in JavaScript
Himanay Khajuria

Himanay Khajuria @himanayk

About: πŸ‘§πŸ½ I'm Frontend Developer based in Stockholm, Sweden, currently working at SITA.dev. My transition into tech has been a unique and rewarding adventure, fueled by curiosity and determination.

Location:
Sweden
Joined:
Feb 22, 2025

πŸ‘¨β€πŸŽ“A Quick Start Guide to RegEx in JavaScript

Publish Date: Feb 26
8 0

Are you struggling to understand Regular Expressions (Regex) in JavaScript? πŸ€” Don't worry! This guide will simplify everything for you.

🀨 What is Regex?

Regex (Regular Expressions) is a search pattern used to match text. It helps in finding, extracting and replacing patterns in strings.

β•°β”ˆβž€ Example:

let text = "Hello, World!";
let pattern = /World/; // A regex to find "World"
console.log(pattern.test(text)); // true βœ…
Enter fullscreen mode Exit fullscreen mode

✨ Basic Regex Methods

Method Description
test() Returns true or false if a match is found. βœ…βŒ
match() Returns an array of matches or null if no match. πŸ“œ

β•°β”ˆβž€ Example of test()

let sentence = "The cat jumps!";
let regex = /cat/;
console.log(regex.test(sentence)); // true βœ…
Enter fullscreen mode Exit fullscreen mode

β•°β”ˆβž€ Example of match()

let sentence = "Rainy days are beautiful!";
let regex = /rainy/i;
console.log(sentence.match(regex)); // [ 'Rainy', index: 0, input: 'Rainy days are beautiful!', groups: undefined ]
Enter fullscreen mode Exit fullscreen mode

πŸ“ Note: i makes the search case-insensitive.


πŸ” Special Characters & Patterns

Symbol Meaning
. Matches any single character except newline.
[] Match any one character inside the brackets.
[^ ] Negated character class (excludes specified characters).
* Matches 0 or more occurrences.
+ Matches 1 or more occurrences.
? Matches 0 or 1 occurrence (lazy match).
{min,max} Matches a range of occurrences.

πŸ”₯ Example: Match Words with . (Wildcard)

let sentence = "The bat, cat, and hat are in the room!";
let regex = /b.t/g; // Matches words like "bat", "bit", "bot" etc.
console.log(sentence.match(regex)); // [ 'bat' ]
Enter fullscreen mode Exit fullscreen mode

πŸ“ Note: g finds all occurrences instead of stopping at the first match.


🚩 Using Flags (g, i, m)

Flag Meaning
g Global match (find all matches). 🌍
i Case-insensitive match. πŸ”‘πŸ” 
m Multiline match. πŸ“œ

πŸ”₯ Example: Case-Insensitive Search with i

let text = "HELLO hello Hello hElLo";
let regex = /hello/ig;
console.log(text.match(regex)); // [ 'HELLO', 'hello', 'Hello', 'hElLo' ]
Enter fullscreen mode Exit fullscreen mode

☣︎ Matching Numbers & Letters

Pattern Description
\d Matches digits (0-9). πŸ”’
\D Matches non-digits. πŸš«πŸ”’
\w Matches letters, numbers, and underscore. πŸ”€
\W Matches non-word characters. πŸš«πŸ”€
\s Matches whitespace (spaces, tabs, newlines). βšͺ
\S Matches non-whitespace characters. πŸ”‘

πŸ”₯ Example: Extracting Digits

let price = "The total is $120.99!";
let regex = /\d+/g;
console.log(price.match(regex)); // ['120', '99']
Enter fullscreen mode Exit fullscreen mode

⏳ Greedy vs. Lazy Matching

🧐 Greedy Matching: Takes as much text as possible.

let str = "abc123def456";
let regex = /\d+/;
console.log(str.match(regex)); // ['123'] (greedy, matches longest possible number)
Enter fullscreen mode Exit fullscreen mode

🧐 Lazy Matching: Stops at the first match.

let regexLazy = /\d+?/;
console.log(str.match(regexLazy)); // ['1'] (lazy, matches shortest possible number)
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Anchors ^ and $

Symbol Meaning
^ Matches start of string. πŸš€
$ Matches end of string. 🏁

πŸ”₯ Example: Matching Start & End

let phrase = "Start here, end there";
let startRegex = /^Start/;
let endRegex = /there$/;
console.log(startRegex.test(phrase)); // true βœ…
console.log(endRegex.test(phrase)); // true βœ…
Enter fullscreen mode Exit fullscreen mode

πŸ“– More Learning Resources ↓

πŸ”— Check out my Technical Presentation
πŸ”— MDN Web Docs


πŸ“‹βœ… Master Regex with this Checklist

πŸ“– Understand what Regex is and how it works πŸ€“

πŸ“– Learn basic Regex methods: test(), match() βœ…

πŸ“– Practice special characters (., *, +, ?, {} etc.) πŸ”£

πŸ“– Explore Regex flags like g, i, m πŸš€

πŸ“– Learn to match numbers, letters and whitespace (\d, \w, \s, etc.) πŸ”’

πŸ“– Understand greedy vs. lazy matching ⏳

πŸ“– Use anchors (^, $) to match start or end of a string 🎯

β™‘ Happy Coding! β™‘

Comments 0 total

    Add comment