DEV Community

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

Posted on β€’ Edited on

3 1 1 2 1

πŸ‘¨β€πŸŽ“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 🎯

β™‘ Happy Coding! β™‘

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video πŸ“ΉοΈ

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay