I’m trying to replace all commas with a comma and a space.
This is what I have tried:
all_boxes_values = all_boxes_values.replace(",", ", ");
// and...
all_boxes_values = all_boxes_values.replace(/,\\/g, ', *');
I tried the top one first but realised it only replaced one instance of the comma, so I tried the bottom one (from a previous S.O question) but cannot get it to work. There is a possibility that the string may not contain any commas in the first place.
Any ideas?
UPDATED
This is the function. I have added one of the below answers to this and it still doesn’t work. I now get a console error: TypeError: Object A A Gill has no method ‘replace’.
$('.name_boxes').live('click', function() {
var all_boxes = $('.name_boxes');
var all_boxes_values = []
for (var i = 0; i < all_boxes.length; i++) {
if (all_boxes[i].checked) {
all_boxes_values.push(all_boxes[i].value)
}
}
all_boxes_values = all_boxes_values.replace(/,/g,", ");
alert(all_boxes_values);
});
You shouldn’t have the last two backslashes in your regular expression, and there is no reason for the
*. The following works.