← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 2 of 26

JS Regular Expressions

A regular expression is a pattern for finding text, like a stencil that only matches certain shapes. You use it to search, check or replace parts of a string.
Syntax
javascript
const regex = /pattern/flags;
const regex = new RegExp("pattern", "flags");
regex.test(string);
string.match(regex);
string.replace(regex, replacement);

What Is Regex?

A regular expression is a pattern used to find or check text. Patterns are written between slashes, like /abc/, and can include flags such as g (global) or i (case-insensitive) that change how the match behaves.

उदाहरण: What Is Regex?

javascript
const pattern = /abc/i;
console.log(pattern.test("ABC123")); // true, case-insensitive flag

Regex Methods

JavaScript provides test() and match() for working with regular expressions. test() returns a boolean indicating whether the pattern matches anywhere in the string, while match() returns the actual matched substring(s) or null if nothing matched.

उदाहरण: Regex Methods

javascript
// Declare the constant `pattern`, set to `/cat/`
// Declare the constant `pattern`, set to `/cat/`
const pattern = /cat/;
// Print `pattern.test("I have a cat")` to the console
// Print `pattern.test("I have a cat")` to the console
console.log(pattern.test("I have a cat"));
// Print `"I have a cat".match(pattern)` to the console
// Print `"I have a cat".match(pattern)` to the console
console.log("I have a cat".match(pattern));

Common Patterns

Character classes and quantifiers help create useful text patterns. Character classes like [a-z] match any character in a range, and quantifiers like + or {2,4} control how many times the preceding piece can repeat.

उदाहरण: Common Patterns

javascript
const pattern = /[a-z]+\d{2,4}/;
console.log(pattern.test("abc123"));

Replace With Regex

replace() can use a regular expression to change matching text. Passing a regex (instead of a plain string) to replace() lets you substitute every match at once when the g flag is set, rather than only the first occurrence.

उदाहरण: Replace With Regex

javascript
const text = "cat cat cat";
console.log(text.replace(/cat/g, "dog"));

Practice Patterns

Start with simple patterns and test different inputs. Regex syntax is dense, so building patterns incrementally against real sample strings — rather than writing the whole pattern blind — makes mistakes much easier to catch.

उदाहरण: Practice Patterns

javascript
// Declare the constant `patterns`, set to `[/^\d+$/, /[A-Z]/, /\s/]`
// Declare the constant `patterns`, set to `[/^\d+$/, /[A-Z]/, /\s/]`
const patterns = [/^\d+$/, /[A-Z]/, /\s/];
// Declare the constant `input`, set to "Test 123"
// Declare the constant `input`, set to "Test 123"
const input = "Test 123";
// Call `patterns.forEach(p => console.log(p, p.test(input)))`
// Call `patterns.forEach(p => console.log(p, p.test(input)))`
patterns.forEach(p => console.log(p, p.test(input)));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.