最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace final comma in a string with "and" - Stack Overflow

programmeradmin4浏览0评论

I've got a string that is generated, and is essentially a list of things. This string is something that will be read by the user, so I'm trying to formate it nicely. I am separating the generated list using mas and spaces:

(a+'').replace(/,/g, ", ");

produces

1, 2, 3, 4

However, I'd like to change the last ma to ", and", so that it reads

1, 2, 3, and 4

I've tried the following:

((a+'').replace(/,/g, ", ")).replace(/,$/, ", and");

but it doesn't work, which I THINK is because that is only looking for mas at the end of the string, rather than the last ma in the string, right?

As well, if there are only 2 items in the string, I want the ma to be replaced with just "and", rather than ", and", to make more sense grammatically.

How can I achieve what I'm looking for?

I've got a string that is generated, and is essentially a list of things. This string is something that will be read by the user, so I'm trying to formate it nicely. I am separating the generated list using mas and spaces:

(a+'').replace(/,/g, ", ");

produces

1, 2, 3, 4

However, I'd like to change the last ma to ", and", so that it reads

1, 2, 3, and 4

I've tried the following:

((a+'').replace(/,/g, ", ")).replace(/,$/, ", and");

but it doesn't work, which I THINK is because that is only looking for mas at the end of the string, rather than the last ma in the string, right?

As well, if there are only 2 items in the string, I want the ma to be replaced with just "and", rather than ", and", to make more sense grammatically.

How can I achieve what I'm looking for?

Share Improve this question asked May 1, 2015 at 10:15 GtwoKGtwoK 5914 silver badges19 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 9

You probably want

,(?=[^,]+$)

for example:

"1, 2, 3, 4".replace(/,(?=[^,]+$)/, ', and');

(?=[^,]+$) checks that there are no more mas after this ma. (?!.*,) would also work.

You can even check there isn't already an and:

,(?!\s+and\b)(?=[^,]+$)

Working example: https://regex101./r/aE2fY7/2

(.*,)

You can use this simple regex.Replace by $1 and or \1 and.See demo.

https://regex101./r/uE3cC4/8

var re = /(.*,)/gm;
var str = '1, 2, 3, 4';
var subst = '$1 and';

var result = str.replace(re, subst);

How about:

((a+'').replace(/,/g, ", ")).replace(/,([^,]*)$/, ", and $1");
var index = a.lastIndexOf(',');
a.replaceAt(index, ', and');

where :

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

You could try

list1=[]
list2=[1, 2, 3, 4]
list2Str=(str(list2).replace("[","").replace("]","").replace("'",""))
for i in range (0,len(list2Str)):
  if list2Str[i] == ',':
    list1.append(i)
list1.sort(reverse=True)
list2Str = list2Str[:list1[0]] + ' and' + list2Str[list1[0]+1:]
print (list2Str)

A bit long-winded but it works and is fairly simple

发布评论

评论列表(0)

  1. 暂无评论