Replace last comma in a list with 'and' in a template

I’ve got a template that pulls together a list of things and returns them comma separated. List items can be multiple words.

Banana, Orange, Apples, Pears, Strawberry

All I want is a template (or rather a function I can pipe into) to replace the last comma with the word and and some sensible spaces

I have tried | regex_replace(find=',([^,]*)$', replace=" and$1" to no avail however that works when trying it in a regex tester.

{{ 'Banana, Oranges, Apples, Pears, Strawberry' | regex_replace(find=',([^,]*)$', replace=" and$1")}} 

Returns

Banana, Oranges, Apples, Pears and$1

I realise it’s an escaping issues and have tried a few things like duplicate backslashes. Nothing seems to work!

It works fine in the tester here.

Any ideas how I can escape properly or any ideas on a different method?

2 Likes