Str is a string with spaces removed:
Delete all spaces:
str = str.replace(/\s+/g," ");
Delete two spaces:
str = str.replace(/^\s+|\s+$/g,“”;
Delete the space on the left:
str=str.replace( /^\s*/,' ');
Delete the space on the right:
str=str.replace(/(\s*$)/g," ");