Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

JavaScript - Regular Expressions

Regular Expressions

 

Regular expressions, often abbreviated as regex or regexp, are powerful tools used for pattern matching and text manipulation. They allow you to search for, match, and manipulate strings based on specific patterns. Regular expressions are supported in many programming languages, including JavaScript, Python, and Java. Here's an introduction to regular expressions in the context of JavaScript:

Creating a Regular Expression:

In JavaScript, you can create a regular expression using either a regular expression literal or the RegExp constructor.

Using a Regular Expression Literal:

let regex = /pattern/;
 

Using the RegExp Constructor:

let regex = new RegExp('pattern');
 

Basic Patterns and Special Characters:

1. Literal Characters: Characters in a regular expression match themselves. For example, the regular expression /cat/ matches the string "cat."

2. Character Classes: Square brackets [] are used to define character classes. [abc] matches either 'a,' 'b,' or 'c.'

3. Quantifiers: Quantifiers specify the number of times a character or group should be matched:

*: 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.

 

4. Metacharacters: Some characters have special meanings in regular expressions and need to be escaped with a backslash (\) to match them literally. For example, \. matches a period, and \\ matches a backslash.

5. Anchors: Anchors are used to match patterns at specific positions:

^: Matches the start of a string.
$: Matches the end of a string.

 

Character Classes and Shorthand:

  • \d: Matches any digit character (equivalent to [0-9]).
  • \D: Matches any non-digit character (equivalent to [^0-9]).
  • \w: Matches any word character (equivalent to [a-zA-Z0-9_]).
  • \W: Matches any non-word character (equivalent to [^a-zA-Z0-9_]).
  • \s: Matches any whitespace character (spaces, tabs, line breaks).
  • \S: Matches any non-whitespace character.

Groups and Alternation:

Parentheses () are used to group characters and create sub-patterns. The vertical bar | represents alternation (logical OR).

/(dog|cat)/
 

Flags:

Regular expressions can have flags that modify their behavior:

  • g: Global - matches all occurrences (not just the first).
  • i: Case-insensitive - ignores case when matching.
  • m: Multiline - treats a string as multiple lines.
let regex = /pattern/gi;
 

Using Regular Expressions in JavaScript:

In JavaScript, you can use regular expressions with methods like test(), match(), search(), replace(), and split() on strings. Here's a basic example:

let str = 'The quick brown fox jumps over the lazy dog.';
let pattern = /fox/gi;

let matches = str.match(pattern); // Returns an array of matches

console.log(matches); // Output: ["fox", "Fox"]