JavaScript - How to Use RegExp in Switch Case Statements?

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To make your JavaScript switch statements more dynamic and flexible, you can use regular expressions (RegExp). Here are the various ways to use RegExp in switch case statement.

1. Using test() Method in switch

The test() method can be used to check if a string matches a given regular expression. Let’s see how this works with a simple switch statement.

JavaScript
let s = "hello world";

switch (true) {
    case /hello/.test(s):
        console.log("The string contains 'hello'");
        break;
    case /world/.test(s):
        console.log("The string contains 'world'");
        break;
    default:
        console.log("No match found");
}

Output
The string contains 'hello'
  • The switch expression evaluates true for each case.
  • The test() method checks if the string input matches the regular expression (e.g., /hello/ or /world/).
  • When a match is found, the corresponding case executes, and we print the matching message.

2. Using match() Method in switch

Alternatively, we can use the match() method, which returns an array of matched substrings or null if no match is found.

JavaScript
let s = "2024-12-03";

switch (true) {
    case s.match(/^\d{4}-\d{2}-\d{2}$/) !== null:
        console.log("The string is a valid date format.");
        break;
    case s.match(/^\d{3}-\d{3}-\d{4}$/) !== null:
        console.log("The string is a valid phone number.");
        break;
    default:
        console.log("No valid format found.");
}

Output
The string is a valid date format.
  • We use the match() method to check if the input string matches a date format (\d{4}-\d{2}-\d{2}) or a phone number format (\d{3}-\d{3}-\d{4}).
  • The match() method returns an array if a match is found, and the corresponding case is executed.
  • If no match is found, the default case is executed.

3. Matching Complex Patterns (Email Validation)

Here’s how you can use a regular expression to validate an email address inside a switch statement.

JavaScript
let s = "example@mail.com";

switch (true) {
    case /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/.test(s):
        console.log("The string is a valid email address.");
        break;
    default:
        console.log("The string is not a valid email address.");
}

Output
The string is a valid email address.
  • The regular expression /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/ matches a valid email format.
  • If the input matches the email pattern, the message "The string is a valid email address" is printed.
  • If no match is found, the default case will output "The string is not a valid email address."

4. Multiple Pattern Matches

You can also use switch to handle multiple patterns by chaining case blocks with different regular expressions.

JavaScript
let s = "https://www.example.com";

switch (true) {
    case /^http/.test(s):
        console.log("The string is an HTTP URL.");
        break;
    case /^https/.test(s):
        console.log("The string is an HTTPS URL.");
        break;
    case /www/.test(s):
        console.log("The string contains 'www'");
        break;
    default:
        console.log("No matching URL pattern found.");
}

Output
The string is an HTTP URL.
  • We use different patterns to check for various parts of a URL, such as http, https, or www.
  • The test() method checks for the pattern match and the corresponding message is displayed based on the match.

Why Use RegExp with switch Statements?

Using RegExp in switch statements can be helpful when

  • You need to match more complex patterns, such as email addresses, phone numbers, or URLs.
  • You want to evaluate multiple potential conditions using a single switch block.
  • You need efficient pattern matching within a switch without requiring multiple if statements.

Limitations and Considerations

  • Performance: While using RegExp inside a switch is powerful, be mindful of performance, especially if you're matching large strings or using complex regular expressions.
  • Matching Multiple Patterns: If you have a large number of patterns to check, the switch with test() or match() can be slower compared to other alternatives like if-else chains.

Next Article

Similar Reads