DEV Community

Cover image for ๐Ÿ‘จโ€๐ŸŽ“A Quick Start Guide to RegEx in JavaScript
Himanay Khajuria
Himanay Khajuria

Posted on

๐Ÿ‘จโ€๐ŸŽ“A Quick Start Guide to RegEx in JavaScript

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 ๐ŸŽฏ

๐Ÿ“– Keep practicing with real-world examples ๐Ÿ’ช

Top comments (0)