可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
You have strings with a separator character (a space or something else) like the following:
"1 2 3"
"1 2"
"1"
What is the best/elegant way in Javascript to format them in the following way?
"1, 2 and 3"
"1 and 2"
"1"
Must works for any number of elements >= 1
回答1:
A basic solution :
function format(input, separator) {
if (!input) return input;
input = input.split(separator || ' ');
if (input.length === 1) return input[0];
return input.slice(0, -1).join(', ') + ' and ' + input.pop();
}
This one sticks to your own approach (trailing separator) :
function format(input, separator) {
var save = input, pattern;
if (!input) return input;
pattern = '[^\\' + (separator || ' ') + ']+';
input = input.match(new RegExp(pattern, 'g'));
if (!input) return save;
if (input.length === 1) return input[0];
return input.slice(0, -1).join(', ') + ' and ' + input.pop();
}
Usage example :
format('1)))2(3)4))', ')'); // "1, 2(3 and 4"
回答2:
Got this somewhere on StackOverflow a few years ago. This is a very basic implemenation -- it wouldn't be hard to add a delimiter parameter to the method.
function formatList(myString){
if ( myString.indexOf(" ") === -1 ) return myString;
if ( myString.indexOf(" ") === myString.lastIndexOf(" ") ){
return myString.replace(" ", " and ");
}
var former = myString.substr(0, myString.lastIndexOf(" "));
former = former.replace(/ /g,", ");
var latter = myString.substr(myString.lastIndexOf(" "), myString.length -1);
var output = former + " and " + latter;
return output;
}
formatList("1"); // 1
formatList("1 2"); // 1 and 2
formatList("1 2 3"); // 1, 2 and 3
formatList("1 2 3 4"); // 1, 2, 3 and 4
回答3:
You can use this:
<!DOCTYPE html>
<html>
<body>
<script>
cars=["Elem 1","Elem 2","Elem 3","Elem 4"];
var s = '';
var sep = '';
for (var i=0;i<cars.length;i++)
{
if(i==cars.length-1){
s=s+" and "+cars[i];
}else{
s=s+sep+cars[i];
sep = ', ';
}
}
document.write(s + "<br>");
</script>
</body>
</html>
回答4:
This one works if you have an array too. It uses the Query $.inArray that can be omitted if you do not use jQuery and input only in array form or string form.
// Transform a string made of substrings separated by a separator (default "#")
// or an array in a string that is comma (", ") separated and that has an "and"
// separaing the last element.
// Ex:
// toCommaAnd("1#2#3") -> "1, 2 and 3"
// toCommaAnd("1 2 3"," ") -> "1, 2 and 3"
// toCommaAnd("Mr. Cinelli#Ms. Cinelli") ->"Mr. Cinelli and Ms. Cinelli"
// toCommaAnd(["a", "b"]) -> "a and b"
//
// !! toCommaAnd("1#2#3#") -> become "1, 2 and 3" TOO so you can
// concatenate elements in loops without
// caring about special case for first or
// last element.
function toCommaAnd(str, separator) {
var ar = $.isArray(str) ? str : str.split(separator || "#"),
commaUntil = ar.length - 1,
ret = ar[0],
i;
//This is just for the 1#2#3# cases.
if (commaUntil > 0 && ar[commaUntil] === ""){
commaUntil--;
}
for (i = 1; i < commaUntil; i++){
ret += ", " + ar[i];
}
if (commaUntil > 0)
ret += " and " + ar[commaUntil];
return ret ;
},
回答5:
This one is pretty twisted :
function format(input, separator) {
var idx = input.indexOf(separator || ' ');
if (idx === -1) return input;
return input.substring(0, idx++) + (
input.indexOf(separator || ' ', idx) === -1 ? ' and ' : ', '
) + (
format(input.slice(idx), separator)
);
}
Fails to handle consecutive and trailing separators though :
format('1|2||3|', '|'); // "1, 2, , 3 and "