1) Using the url encoding method directly, the spaces can be escaped as "%20".
function replaceSpace(str) { return encodeURIComponent(str); }
2) is to use regular matching to find empty characters, and then replace them. (the replaceAll method exists in java, but not in js)
function replaceSpace(str) { return str.replace(/\s/g,'%20'); }
3) Use a space to split the characters into an array, then use the character '%20' to link the array into one character.
function replaceSpace(str) { var splitArr = p.split ('' ); return splitArr.join ('% 20' ); }
4) Loop the string character, if it is not undefined and not a space, it will be directly linked to the new string, if it is a space, the link character "%20" will be linked to newStr.
function replaceSpace(str) { var newStr = '', i, len = str.length; for(i=0;i<len;i++) { if(str[i] != 'undefined' && str[i] !== ' ') { newStr += str[i]; }else { newStr += '%20'; } } return newStr; }