Regular Expressions 101

Community Patterns

Remove trailing commas of PHP array and functions

1

Regular Expression
ECMAScript (JavaScript)

/
([\]\)]),(\s*\n*\s*)([\]\)])
/
gm

Description

This expression looks for sequences of PHP arrays, either closed with ] or ) with unnecessary trailing commas and captures the content divided into three parts, except by the comma itself. You can use it to search an replace in editors like VSCode like this:

Search: ([\])]),(\s*\n*\s*)([\])])

Replace: $1$2$3

This should help update the compatibility of your code with versions previous to 7.3.

Submitted by Mateus Machado Luna - 2 years ago