Javascript - Permalinks converting
===

I use JavaScript to convert my string to a nice permalink for URL, but i have a small problem when i use duble or more space its convert it to -- and more, if i use it in the end of this string its replace it to - to, like this 

    this---is---a-test-right-now----

the string i want to have back if i got this is follow

    this-is-a-test-right-now

can sombardy help me here?

my own Javascript look like this

    function prototype( str )
    {
        var ret = str;
        ret = ret.replace( /ø/g, 'oe' );
    	ret = ret.replace( /Ø/g, 'OE' );
    	ret = ret.replace( /å/g, 'aa' );
    	ret = ret.replace( /Å/g, 'AA' );
    	ret = ret.replace( /æ/g, 'ae' );
    	ret = ret.replace( /Æ/g, 'AE' );
    	ret = ret.replace( /\_/g, '-' );

		ret = ret.replace(/[^a-zA-Z0-9\/-]/ig,'-').replace(/_+/ig,'-').toLowerCase();
	
		return ret;
	}