Here's the string:
var myString = "apple, 0.90, pear, 1.23, orange, 1.90";
What regular expression do I use to change the string to this:
apple: 0.90, pear: 1.23, orange: 1.90
I want to replace the comma after the end of the fruit to a colon.
Here's the string:
var myString = "apple, 0.90, pear, 1.23, orange, 1.90";
What regular expression do I use to change the string to this:
apple: 0.90, pear: 1.23, orange: 1.90
I want to replace the comma after the end of the fruit to a colon.
You can split the string into an array and then make new string. Like this:
var myString = "apple, 0.90, pear, 1.23, orange, 1.90";
var array = myString.split(', ');
var output = '';
for (var i = 0; i < array.length; i += 2) {
output += array[i] + ': ' + array[i + 1];
output += (i - 2 == array.length) ? '' : ', ';
}