Javascript Replace Globally

I have a bit of javascript

function hasSpaces(name)
{
if(name.search(/^\s*/) != -1){
alert(name);
name.replace((/^\s*/, "~"))
alert(name);
}
return name;
}

The name comes in like john smith and i have to turn it to john~smith, the alerts are just for testing. Sorry if this is the wrong forum but i need help and am terrible at javascript. It also has to replace all spaces in the name in case the name is john adams smith to john~adams~smith

thanks
Sean

Insania,
We know about unix -- Java in not in the plate.
If you are lucky, you will find somebody here with Java experience.
Good luck!

Javascript is not Java.

The following example shows how to perform a global replace of whitespace character to "~".

var spx = /\s/g;
alert("adam smith king".replace(spx, "~"));

See:
http://developer.mozilla.org/en/docs/Core\_JavaScript\_1.5\_Reference:Global_Objects:RegExp