Extract Text Before First Separating Character (Like Comma Or Space) With Regex

Category: Strings

In my last post, I shared a regex that extracts characters between parentheses. This time, I am sharing a regular expression to get the text before the first separating character (like comma, space, etc).

Many times in web development projects, we get the strings that are already separated by some characters. So it is often required to find the text before first separating character.

I found a regular expression that works quite well in order to retrieve the data before the first separating character. I am pretty sure that this can be generalized for other similar situations. Please leave your comment for me if you have some use cases in your project where you have used it.

Note that this regex shown below will extract text before the first comma (,).

/^([^,])+/g

Matches:

  • Regex, Pattern, Com
  • RegexPattern,Com
  • Regex, Pattern Com

Non-matches:

  • Regex.Pattern.Com
  • Regex Pattern Com

See Also:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.