我有个问题,想知道如何替换字符串中最后一个逗号后面的空格为 "and":
给定这个字符串:test1, test2, test3
期望得到以下结果: test1, test2 and test3
我尝试了如下方法:
给定这个字符串:test1, test2, test3
期望得到以下结果: test1, test2 and test3
我尝试了如下方法:
var dialog = 'test1, test2, test3';
dialog = dialog.replace(new RegExp(', /g').lastIndex, ' and ');
但它没有起作用。
\w
更全面,例如:http://jsbin.com/utedu/2 - T.J. Crowdertest1, test2,
并为其生成了test1, test2 and
。同时,它在原始输入字符串中插入了一个多余的空格字符,因为它忘记了匹配,
后面的空格字符。 - splash