Regular Expression To Match Anything Before Underscore

Category: Strings

This is a quick regular expression that matches all content before an underscore(_) in a string.

Can be useful in replacing all characters before an underscore in a set of usernames.

/^[^_]*_/g

Matches:

  • regex_pattern
  • 123_456
  • abc_123_456

Non-matches:

  • regex-pattern
  • regex pattern
  • regexpattern

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.