Sitemap

JavaScript Regex: 20 Practical Examples

3 min readMay 13, 2023

--

Press enter or click to view image in full size
Photo by Ferenc Almasi on Unsplash

Regular expressions are a powerful tool for working with strings in JavaScript, allowing you to search, match, and manipulate text with ease. In this blog post, we’ll explore 20 practical examples of JavaScript regular expressions.

1. Basic Matching

const pattern = /hello/;
const text = "Say hello to the world!";
console.log(pattern.test(text)); // true

2. Case-Insensitive Matching

const pattern = /hello/i;
const text = "Say Hello to the world!";
console.log(pattern.test(text)); // true

3. Global Matching

const pattern = /hello/g;
const text = "Hello, hello, hello!";
console.log(text.match(pattern)); // ["hello", "hello", "hello"]

4. Matching Any Single Character

const pattern = /h.llo/;
const text = "Say hxllo to the world!";
console.log(pattern.test(text)); // true

5. Matching Any Character (Wildcard)

const pattern = /h.*o/;
const text = "Say h3ll0 to the world!";
console.log(pattern.test(text)); // true

6. Matching the Beginning of a String

const pattern = /^hello/;
const text = "hello world!";
console.log(pattern.test(text)); // true

7. Matching the End of a String

const pattern = /world!$/;
const text = "hello world!";
console.log(pattern.test(text)); // true

8. Matching Word Boundaries

const pattern = /\bhello\b/;
const text = "Say a big hello!";
console.log(pattern.test(text)); // true

9. Matching a Range of Characters

const pattern = /[a-zA-Z]/;
const text = "123abc456";
console.log(pattern.test(text)); // true

10. Matching a Specific Number of Characters

const pattern = /\d{4}/;
const text = "Year: 2022";
console.log(pattern.test(text)); // true

Get JavaScript-World’s stories in your inbox

Join Medium for free to get updates from this writer.

11. Matching One or More Characters

const pattern = /h.+o/;
const text = "hello world!";
console.log(pattern.test(text)); // true

12. Matching Zero or More Characters

const pattern = /h.*o/;
const text = "ho world!";
console.log(pattern.test(text)); // true

13. Matching Either/Or

const pattern = /hello|world/;
const text = "Say hello and world!";
console.log(pattern.test(text)); // true

14. Capturing Groups

const pattern = /(hello) (world)/;
const text = "hello world!";
const match = pattern.exec(text);
console.log(match[1]); // "hello"
console.log(match[2]); // "world"

15. Non-Capturing Groups

const pattern = /(?:hello) (world)/;
const text = "hello world!";
const match = pattern.exec(text);
console.log(match[1]); // "world"

16. Lookahead Assertions

const pattern = /hello(?= world)/;
const text = "hello world!";
console.log(pattern.test(text)); // true

17. Negative Lookahead Assertions

const pattern = /hello(?! world)/;
const text = "hello earth!";
console.log(pattern.test(text)); // true

18. Lookbehind Assertions

const pattern = /(?<=say )hello/;
const text = "say hello!";
console.log(pattern.test(text)); // true

19. Negative Lookbehind Assertions

const pattern = /(?<!say )hello/;
const text = "greet hello!";
console.log(pattern.test(text)); // true

20. Replacing Text with Regular Expressions

const pattern = /hello/;
const text = "Say hello to the world!";
const newText = text.replace(pattern, "hi");
console.log(newText); // "Say hi to the world!"

These examples demonstrate the versatility of JavaScript regular expressions. By mastering the basics and exploring their advanced features, you can elevate your string manipulation skills in JavaScript. Regular expressions may seem complex at first, but with practice, they become an essential tool for any JavaScript developer.

If you enjoyed the article and would like to show your support, be sure to:

👏 Applaud for the story (50 claps) to help this article get featured

👉Follow me on Medium

Check out more content on my Medium profile

--

--

JavaScript-World
JavaScript-World

Written by JavaScript-World

Elevating web experiences with 10+ yrs in JS🚀.Crafting seamless UIs,optimizing performance, mentoring next-gen coders.Passionate about learning&innovation.🌐✨