What regex will match every character except comma "," or semi-colon ";"?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What regex will match every character except comma "," or semi-colon ";"?

Regex Made Easy: A Guide to Matching Every Character Except Comma or Semicolon

Are you struggling to find a regex that can match every character except a certain defined character or set of characters? Look no further! 🙌 In this blog post, we'll explore a common issue encountered by developers and provide you with easy solutions to help you conquer this problem once and for all! So, let's dive in! 💻

Understanding the Problem

Recently, I had a situation where I needed to split a string by either a comma (,) or a semicolon (;). After some brainstorming, I realized that using a regex to match everything until it encountered a comma or a semicolon could be an elegant solution to my problem. But how can we achieve this? 🤔

Solution 1: Negated Character Class

One way to match every character except a comma or semicolon is by using a negated character class in our regex pattern. A negated character class is denoted by the caret (^) symbol followed by the characters we want to exclude. So, to exclude comma and semicolon, our regex pattern will be:

[^,;]

This pattern will match any character that is not a comma or semicolon. Cool, right? 😎

Here's an example to illustrate this solution:

const str = "Hello, World; This is a test!";
const regex = /[^,;]/g;

const result = str.match(regex);
console.log(result);

The output of this code will be an array with all the characters in the string, except for the comma and semicolon:

["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", " ", "T", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t", "!"]

Solution 2: Negative Lookahead

Another approach is to use a negative lookahead in our regex pattern. A negative lookahead is denoted by the (?!...) syntax. It allows us to match a position in the string that is not followed by a specific pattern. In our case, we want to match any character that is not followed by a comma or semicolon. So, our regex pattern will be:

.{1}(?![,;])

Let's break down the parts of this pattern:

  • .{1} matches any single character.

  • (?![,;]) is a negative lookahead that ensures the character is not followed by a comma or semicolon.

Here's an example to demonstrate this solution in action:

const str = "Hello, World; This is a test!";
const regex = /.{1}(?![,;])/g;

const result = str.match(regex);
console.log(result);

The output will be the same as the previous example:

["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", " ", "T", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t", "!"]

Call-to-Action: Level Up Your Regex Skills!

Regex can be a powerful tool in your coding arsenal, and mastering it can save you valuable time and effort. Now that you know how to match every character except a comma or semicolon, why not explore more regex patterns and unleash your full potential? 😃

Share your favorite regex tricks in the comments below and let's help each other level up our regex skills! Happy coding! 🚀🔥

*[g]: global flag

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my