1. Home
image

JavaScript Tutorial Concepts - From Beginner to Pro

Learn JavaScript from scratch! Our tutorial covers basics to advanced concepts. Start learning today!

  • 24
  • 9 Hours
right-top-arrow
24

JavaScript Regex

Updated on 13/08/20244,334 Views

A lot of developers I know started out finding JavaScript regex incredibly complicated. The cryptic symbols and complex rules seemed overwhelming for beginners. However, with practice and a gradual approach, all the JS developers I know gained a solid understanding of regex's power in JavaScript.

All of my fellow JS developers can confidently use it for tasks like validation and text manipulation now. If you master JavaScript regex, you gain significant control and power over JS-based development.

Let us learn all the essential concepts related to regex in JS.

What are Regular Expressions (Regex)?

Regular expressions (or regex for short) are specialized sequences of characters that define a flexible search pattern within text. We can think of them as a sophisticated "find and replace" tool on steroids. Regex goes way beyond literal matching, it allows us to describe patterns based on character types, positions, repetitions, and more.

At their core, regular expressions are like a mini-language. This language uses a combination of normal characters (for literal matches) and special characters called metacharacters. These metacharacters  (like ".", "*", "+") give us immense control in defining complex patterns. For instance, the regex \d{3}-\d{2}-\d{4} could describe an identitification number.

Regex is used in text manipulation for:

  • Searching: Regex allows us to locate text patterns with extraordinary precision, whether it's finding specific words, email addresses, or complex data formats.
  • Replacing: We can use regex to find patterns and substitute them with new text, allowing for efficient text transformations.
  • Validation: Regex is widely used to create robust input validation rules, ensuring that data like phone numbers, passwords, or website URLs adhere to specific formats.

JavaScript Regex Implementation

JavaScript offers two primary ways to work with regular expressions:

  1. The RegExp Object: This is the core of regex functionality. We create a RegExp object to define your pattern and apply it using methods. There are two ways to create it:
  • Regex literals: Enclose the pattern between slashes ( /pattern/flags ). This is useful for patterns known in advance.
  • RegExp constructor: Use new RegExp('pattern', 'flags') for dynamic patterns built during runtime.

Here are the RegExp methods

  • .test(string): Returns true if a match is found within the string, otherwise false. Handy for basic existence checks.
  • .exec(string): Powerful but complex.  Returns detailed information about a single match (index, groups, etc.) or null if there's no match.  With the 'g' flag, it can be used repeatedly to get successive matches.
  1. String Methods: Several built-in String methods leverage regex power:
  • search(regexp): Finds the first match. Returns the index of the match, or -1 if not found. Doesn't consider flags (like 'g') of the regex.
  • .match(regexp): Has two behaviors:
    • Without 'g' flag: Returns an array with the first match, its index, groups, etc., or null if nothing matches.
    • With 'g' flag: Returns an array of all the matched strings.
  • .matchAll(regexp): Requires the 'g' flag.  Returns an iterator to get information about each successive match.
  • .replace(regexp, replacement): Replaces matches within the string. The replacement can be a string or a function to compute the replacement.
  • .replaceAll(regexp, replacement): Same as .replace(), but with the 'g' flag implicitly set, replacing all matches.
  • .split(regexp): Splits the string into an array of substrings, using the regex as the delimiter.

Creating Regular Expressions

Let us learn how to create these expressions with JavaScript regex examples. We will explore the two methods I mentioned earlier.

Regular Expression Literals

Syntax: Enclose your pattern between forward slashes, followed by optional flags: /pattern/flags

Pros: Simple and concise for patterns that don't change during execution.

Example: /hello/ would match the literal word "hello".

The RegExp Constructor

Syntax: new RegExp('pattern', 'flags')

Pros: Allows you to build patterns dynamically based on variables or user input. You pass the pattern and flags as strings.

Important note: When using the constructor, you need to escape backslashes twice (e.g., \\d to represent a digit).

Example:

let digits = "123";

let regex = new RegExp('\\d+' + digits, 'g'); 

// This would match "123" and other digit sequences.

Basic Regex Patterns

Let us now learn the basic JavaScript regex patterns.

Literal Characters

The most straightforward elements such as regular letters, numbers, and some symbols match themselves, as we already covered in creating regular expressions.

Metacharacters

There are the metacharacters:

  • .: The wildcard! It matches any single character except a newline. Example: .at matches "cat", "hat", etc.
  • \d: Shorthand for a single digit (equivalent to [0-9]).
  • \w: Matches word characters: letters, digits, and underscore.
  • \s: Matches whitespace: spaces, tabs, newlines, etc.
  • \t: Specifically matches a tab character.

Character Classes

These provide ways to match sets or ranges of characters:

  • [abc]: Matches a single character from the set inside the brackets. Example: [xyz] matches 'x', 'y', or 'z'.
  • **[^abc]: ** Negates the set. Matches any single character not inside the brackets.
  • [a-z]: Matches any single lowercase letter from 'a' to 'z'.
  • [0-9]: Matches any single digit from '0' to '9'.

Quantifiers

Let us learn about quantifiers in JavaScript regex.

Greedy Quantifiers

These quantifiers aim to match as much text as possible by default:

  • *: Zero or more repetitions. It will match as many times as it can, even if zero matches are valid.
  • +: One or more repetitions. It requires at least one match to be successful.
  • ?: Zero or one repetition. Effectively makes the preceding element optional.

Specific Repetition

For finer control, use curly braces:

  • {n}: Matches exactly 'n' repetitions of the previous element.
  • {n,}: Matches at least 'n' repetitions, with no upper limit.
  • {n,m}: Matches between 'n' and 'm' repetitions (inclusive).

Lazy Quantifiers (non-greedy)

Lazy quantifiers match the shortest possible substring that satisfies the pattern. This is useful when you don't want to "overshoot" a match.

Add a ? after a greedy quantifier to make it lazy: (*?, +?, ??)

If you wish to learn javascript Regex online and all the other important JS concepts, you can enroll in the full stack development courses by upGrad.

Groups and Backreferences

Let us now learn about groups and backreferences in JavaScript regex.

Capturing Groups (())

Parentheses () create capturing groups, allowing you to "remember" parts of a regex match:

  • Each group gets a number (left to right by opening parenthesis).
  • You can access captured text with backreferences: \1 for the first group, \2 for the second, etc.

Non-capturing Groups (?:)

Groups without capturing, for when you want to group elements but don't need to reference them later.

Example:

  • Regex: (\w+)\s(\w+)
  • Text: "hello world"

The above regex has two capturing groups. \1 would reference "hello", \2 would reference "world"

Anchors

These assert positions within the text, not matching characters:

  • ^ - Beginning of the String: Matches only if the pattern is at the absolute start of the string.
  • $ - End of the String:  Matches only if the pattern is at the absolute end of the string.
  • \b - Word Boundary: Matches the position between a word character and a non-word character or at the beginning/end of the string if it's a word character.

Example:

/^\d{3}$/ matches exactly a 3-digit number at the beginning of the input.

Flags

Flags modify how regex matching behaves:

  • g: Global, finds all matches.
  • i: Case-insensitive, ignores letter case.
  • m: Multiline, treats start (^) and end ($) of line anchors differently.

JavaScript Regex Use Cases

Let's explore some real-world use cases of regular expressions.

Email Validation

A basic email validation regex might look like this: /^[\w-.+]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ 

This pattern checks for an optional string of word characters, dots, hyphens, and plus signs before the '@', followed by a domain name and top-level domain. A more robust regex would be needed for full email standard compliance.

Password Strength Validation

Here is a password strength validation regex:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\-__+.]).{8,}$/

The above code looks complicated, but it uses lookaheads (we will cover this soon in the later section) to enforce:

  • At least one digit
  • At least one lowercase letter
  • At least one uppercase letter
  • At least one special character
  • A minimum length of 8

Form Input Validation

  • Phone Numbers: /^\d{3}-\d{3}-\d{4}$/ (US format)
  • ZIP Codes: /^\d{5}(?:-\d{4})?$/ (US basic and extended)
  • Other inputs such as URLs, identification numbers, etc. each have their own potential patterns

Extracting Data from Text

Find all dates in a format: /(\d{2}\/\d{2}\/\d{4})/g

Capturing groups would let you extract month, day, year separately

Complex Search and Replace

Wrap HTML image names in <img> tags:

Search: (\w+\.(?:jpg|gif|png))

Replace: <img src="$1" alt="$1" />

Advanced JavaScript Regex Topics

Before we delve into some advanced JavaScript regex concepts, I would want to mention that lookarounds can make regex harder to read, so they should be used strategically. Also, overly complex regex can impact performance, so you should test with realistic data.

Lookaheads and Lookbehinds

These allow you to assert what should be around a match without including it in the matched text. They use a special syntax:

  • Positive Lookahead (?=pattern): Checks if a pattern is followed by the lookahead but doesn't make it part of the match.
  • Negative Lookahead (?!pattern): Checks if a pattern is not followed by the lookahead.
  • Positive Lookbehind (?<=pattern): Checks if a pattern is preceded by the lookbehind.
  • Negative Lookbehind (?<!pattern): Checks if a pattern is not preceded by the lookbehind.

Let's revisit our password regex using lookaheads,

Example: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()\-__+.]).{8,}$/

Each (?=.*pattern) asserts a condition must be true somewhere ahead but doesn't capture that part of the match.

Alternation ( | )

The 'pipe' symbol (|) acts as an "OR" operator within a regex.

Example: /hello|goodbye|hola/ would match any of the words "hello", "goodbye", or "hola".

Complex Patterns

Combining complex patterns with other techniques opens up many possibilities such as validating variable or function names. A regex might combine alternation, lookarounds, and character classes to ensure specific naming rules are met.

JavaScript Regex Match Example Program

Here is an example of JavaScript regex matches:

Code:

function validateEmail(email) {

  // A relatively basic email validation regex:

  const regex = /^[\w-.+]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i; 

  return regex.test(email);

}

// Example usage:

const email1 = "hello@example.com";

const email2 = "not-an-email";

if (validateEmail(email1)) {

  console.log("Valid email!");

} else {

  console.log("Invalid email.");

}

if (validateEmail(email2)) {

  console.log("Valid email!"); 

} else {

  console.log("Invalid email."); 

}

Best Practices and Tips

Using JavaScript Regex Test Platforms

They are invaluable! Testers provide real-time feedback as you build and experiment with patterns. Here are some my favorite choices for JavaScript regex maker and tester experience:

Start Simple, Build Up

  • Break complex problems into smaller regexes.
  • Test each stage! Once working, combine them carefully.
  • This makes your regex more readable and easier to debug.

Be Mindful of Performance

Overly complex regexes can hurt performance, especially on large datasets. Here are some potential red flags:

  • Deeply nested capturing groups
  • Excessive backtracking due to ambiguous patterns
  • Lookbehinds used heavily (they can be computationally expensive)

When possible, consider alternative (non-regex) solutions for very complex tasks.

Wrapping Up

Regex is a valuable tool in any JavaScript development arsenal for working with text. The initial learning curve might be steep, but the rewards in terms of efficiency and flexibility are well worth the effort.

If you wish to master technologies such as JavaScript, you can enroll in upGrad’s software engineering courses.

Frequently Asked Questions

  1. Why regex is used in JavaScript?

Regex in JavaScript is used for pattern matching, searching, and manipulating text strings.

  1. What are the limitations of regular expression in JavaScript?
  • JavaScript regex can become complex and hard to read.
  • Some advanced features found in other languages might be missing.
  • Overly complex regex can impact performance.
  1. What are the methods of regex in JavaScript?
  • String methods: .search(), .match(), .matchAll(), .replace(), .replaceAll(), .split()
  • RegExp methods: test(), .exec()
  1. What are the uses of regular expression?
  • Input validation (emails, passwords, etc.)
  • Text extraction and manipulation
  • Advanced search and replace operations
  • Implementing code syntax highlighting
  1. What are the benefits of regex?

Regex offers concise and powerful pattern-matching in a single expression. It also saves development time for tasks that would otherwise require complex string manipulation code.

  1. Where to use regex in JavaScript?

Any task involving string validation, searching, or text manipulation. We can also use it for form input fields, URL parsing, log analysis, etc.

  1. How to check if regex exists in JavaScript?

You can create a RegExp object to represent the pattern:

let myRegex = /hello/; 

  1. How to check if regex matches JavaScript?

Use the .test() method on the RegExp object:

myRegex.test("hello world"); // Returns true 

  1. What regex means?

Regex stands for "Regular Expression". It's a sequence of characters defining a specific search pattern.

image

mukesh

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...