Search⌘ K
AI Features

Regular Expressions in JavaScript

Explore the fundamental concepts of writing regular expressions in JavaScript. Understand the two main syntax formats, how to create RegExp objects, and apply modifiers for case-insensitive, global, and multiline matching. This lesson helps you master practical uses of RegEx to enhance pattern matching skills in JavaScript.

We'll cover the following...

We know that literals and positional/frequency quantifiers are used to create more elaborate patterns. In this lesson, we will how to write RegEx in JavaScript.

Syntax in JavaScript

There are two formats when writing a RegEx in JavaScript.

Node.js
var regex1 = /[a-b]*/; // Create a regex and assign to regex1
var regex2 = RegExp('[a-b]*'); // Create a regex and assign to regex2
console.log(regex1,regex2); // Print the regex variables

In the first format (line 1), use the / operator to tell JavaScript to create a RegEx. ...