var abc="a b c d".split(/\s/);var new1=abc.join(",").replace(/,?d/,"");if(new1.substring(0,1)==","){new1=new1.substring(1)};new1=new1.replace(/,/ig," ");alert (new1);