Regular Expression To Match Anything Before Underscore

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: