arrays - Insert few characters before character in a string variable (javascript) -


i have following javascript variable.

var data = "ashley, andy, juana" 

i want above data this.

var data = "sports_ashley, sports_andy, sports_juana" 

it should dynamic in nature. number of commas can present in variable.

can let me easy way achieve please.

using .replace should work add sports before each comma. below have included example.

var data = data.replace(/,/g , ", sports_"); 

in example using regexp g flag replace commas sports, instead of first occurrence.

then @ end should able append sports end so.

data = "sports_" + data; 

Comments