Skip to content

JavaScript regex examples

  • by

JavaScript regex examples serve as practical demonstrations of how to use regular expressions in JavaScript code. By showcasing various scenarios and patterns, these examples illustrate the versatility and power of regex in JavaScript programming.

They help developers understand the syntax, constructs, and available options for pattern matching, data extraction, and text manipulation.

With JavaScript regex examples, developers can learn how to:

  • Match specific patterns or substrings within a larger text.
  • Perform case-insensitive matching to disregard letter case.
  • Extract data using capturing groups to retrieve specific portions of a matched pattern.
  • Use metacharacters like dot (.) to match any character or square brackets ([]) to match character ranges.
  • Apply quantifiers to specify the number of occurrences or repetitions of a pattern.
  • Enhance string validation, data parsing, and text manipulation tasks using regex.

Here’s an overview of the basic syntax for writing regular expressions in JavaScript:

1. Creating a regular expression: You can create a regular expression by enclosing the pattern between forward slashes (/). For example:

const regex = /pattern/;

2. Matching: To check if a pattern matches a string, you can use the test() method of the regular expression object. It returns true if there is a match and false otherwise.

const regex = /pattern/;
const text = 'Some text';

console.log(regex.test(text)); // Output: true or false

3. Matching any character: The . (dot) metacharacter matches any single character except newline characters.

const regex = /h.t/;
const text = 'hat, hit, hot';

console.log(text.match(regex)); // Output: ['hat']

4. Character classes: Character classes allow you to define a set of characters that can match a single character at that position. You can use square brackets ([]) to define a character class.

const regex = /[aeiou]/;
const text = 'Hello, World!';

console.log(text.match(regex)); // Output: ['e', 'o', 'o']

5. Quantifiers: Quantifiers specify the number of occurrences or repetitions of a preceding pattern. Here are some common quantifiers:

*: Matches zero or more occurrences.
+: Matches one or more occurrences.
?: Matches zero or one occurrence.
{n}: Matches exactly n occurrences.
{n,}: Matches n or more occurrences.
{n,m}: Matches between n and m occurrences.

For example:

const regex = /go{2}gle/;
const text = 'google';

console.log(regex.test(text)); // Output: true

6. Capturing groups: Parentheses () are used to create capturing groups. They allow you to extract specific portions of a matched pattern.

const regex = /(\d{2})-(\d{2})-(\d{4})/;
const text = 'Date: 20-06-2023';

const match = text.match(regex);
console.log(match[1]); // Output: '20'
console.log(match[2]); // Output: '06'
console.log(match[3]); // Output: '2023'

These are some of the basic syntax elements for JavaScript regular expressions. Regular expressions offer much more functionality and options, including modifiers, anchors, escape characters, and more.

JavaScript regex examples

Here are some examples of regular expressions (regex) in JavaScript:

1. Matching a specific pattern:

const regex = /hello/;
const text = 'Hello, World!';

console.log(regex.test(text)); // Output: false

2. Case-insensitive matching:

const regex = /hello/i;
const text = 'Hello, World!';

console.log(regex.test(text)); // Output: true

3. Matching multiple occurrences of a pattern:

const regex = /ab*/;
const text = 'abbabbbb';

console.log(text.match(regex)); // Output: ['abb', 'a']

4. Matching any character using dot (.) metacharacter:

const regex = /h.t/;
const text = 'hat, hit, hot';

console.log(text.match(regex)); // Output: ['hat']

5. Matching a range of characters using square brackets ([]):

const regex = /[aeiou]/;
const text = 'Hello, World!';

console.log(text.match(regex)); // Output: ['e']

6. Matching a specific number of occurrences using quantifiers:

const regex = /go{2}gle/;
const text = 'google';

console.log(regex.test(text)); // Output: true

7. Extracting groups using parentheses (capturing groups):

const regex = /(\d{2})-(\d{2})-(\d{4})/;
const text = 'Date: 20-06-2023';

const match = text.match(regex);
console.log(match[1]);
console.log(match[2]); 
console.log(match[3]); 

Output:

JavaScript regex examples

These are just a few examples of what you can do with regular expressions in JavaScript. Regular expressions are a powerful tool for pattern matching and manipulation of text.

Comment if you have any doubts or suggestions on this Js RegEx topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading