如何检查字符串的最后2个字符是否为},如果是,则从中删除逗号?

I'm looking for a regex pattern which will filter a string.

If the last 2 characters are ,} it should remove the , from the string.

Example:

helloworld,}
        //^

should become

hellworld}   
       //^

If you insist on regex, you can use positive look ahead as

preg_replace('/,(?=}$)/', '', "helloworld,}")
// helloworld}

Regex Explanation

  • , Matches ,

  • (?=}$) positive look ahead. Checks if the , is followed by } and then end of line $