Regex To Match Comma Before & After A String

Category: Strings

A regular expression that allows you to match any comma before and/or after a string. Can be useful for removing all unexpected commas in an array.

Comma Before A String:

/^,|,$/

Matches:

  • ,regex,pattern,com
  • ,,regex,pattern,com

Comma Before And After A String:

/^,+|,+$/

Matches:

  • ,regex,pattern,com
  • regex,pattern,com,
  • ,,regex,pattern,com,

Has Whitespace:

/^[,\s]+|[\s,]+$/

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.