alert("this is a test".split(" ").join("-"));

//or

title.replace(/\s/g , "-");