JavaScript Regex: 20 Practical Examples
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)); // true2. Case-Insensitive Matching
const pattern = /hello/i;
const text = "Say Hello to the world!";
console.log(pattern.test(text)); // true3. 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)); // true5. Matching Any Character (Wildcard)
const pattern = /h.*o/;
const text = "Say h3ll0 to the world!";
console.log(pattern.test(text)); // true6. Matching the Beginning of a String
const pattern = /^hello/;
const text = "hello world!";
console.log(pattern.test(text)); // true7. Matching the End of a String
const pattern = /world!$/;
const text = "hello world!";
console.log(pattern.test(text)); // true8. Matching Word Boundaries
const pattern = /\bhello\b/;
const text = "Say a big hello!";
console.log(pattern.test(text)); // true9. Matching a Range of Characters
const pattern = /[a-zA-Z]/;
const text = "123abc456";
console.log(pattern.test(text)); // true10. Matching a Specific Number of Characters
const pattern = /\d{4}/;
const text = "Year: 2022";
console.log(pattern.test(text)); // trueGet 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)); // true12. Matching Zero or More Characters
const pattern = /h.*o/;
const text = "ho world!";
console.log(pattern.test(text)); // true13. Matching Either/Or
const pattern = /hello|world/;
const text = "Say hello and world!";
console.log(pattern.test(text)); // true14. 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)); // true17. Negative Lookahead Assertions
const pattern = /hello(?! world)/;
const text = "hello earth!";
console.log(pattern.test(text)); // true18. Lookbehind Assertions
const pattern = /(?<=say )hello/;
const text = "say hello!";
console.log(pattern.test(text)); // true19. Negative Lookbehind Assertions
const pattern = /(?<!say )hello/;
const text = "greet hello!";
console.log(pattern.test(text)); // true20. 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
