foo.replace(/,([^,]*)$/, ' and $1')
使用
$( 行尾 )锚点来指定您的位置,并在逗号索引的右侧查找不包含任何其他逗号的模式。
编辑:
以上内容完全符合定义的要求(尽管替换字符串任意松散),但基于评论意见,以下内容更好地体现了原始要求的精神。
console.log( 'test1, test2, test3'.replace(/,s([^,]+)$/, ' and $1'))

foo.replace(/,([^,]*)$/, ' and $1')
使用
$( 行尾 )锚点来指定您的位置,并在逗号索引的右侧查找不包含任何其他逗号的模式。
编辑:
以上内容完全符合定义的要求(尽管替换字符串任意松散),但基于评论意见,以下内容更好地体现了原始要求的精神。
console.log( 'test1, test2, test3'.replace(/,s([^,]+)$/, ' and $1'))