admin管理员组

文章数量:1134247

Is there a way to replace every "%20" with a space using JavaScript. I know how to replace a single "%20" with a space but how do I replace all of them?

var str = "Passwords%20do%20not%20match";   
var replaced = str.replace("%20", " "); // "Passwords do%20not%20match"

Is there a way to replace every "%20" with a space using JavaScript. I know how to replace a single "%20" with a space but how do I replace all of them?

var str = "Passwords%20do%20not%20match";   
var replaced = str.replace("%20", " "); // "Passwords do%20not%20match"
Share Improve this question edited Nov 21, 2016 at 2:14 Alexis Tyler 9296 gold badges32 silver badges50 bronze badges asked Dec 26, 2013 at 23:23 user3109009user3109009 8251 gold badge6 silver badges6 bronze badges 2
  • 4 You can use window.decodeURIComponent(), it will convert more than just '%20' though. – Matt Greer Commented Dec 26, 2013 at 23:28
  • 3 No, this isn't an exact duplicate. There is a deeper issue, and there is an answer here that successfully addresses that issue. – Makoto Commented Dec 27, 2013 at 0:01
Add a comment  | 

7 Answers 7

Reset to default 173

Check this out: How to replace all occurrences of a string in JavaScript?

Short answer:

str.replace(/%20/g, " ");

EDIT: In this case you could also do the following:

decodeURI(str)

The percentage % sign followed by two hexadecimal numbers (UTF-8 character representation) typically denotes a string which has been encoded to be part of a URI. This ensures that characters that would otherwise have special meaning don't interfere. In your case %20 is immediately recognisable as a whitespace character - while not really having any meaning in a URI it is encoded in order to avoid breaking the string into multiple "parts".

Don't get me wrong, regex is the bomb! However any web technology worth caring about will already have tools available in it's library to handle standards like this for you. Why re-invent the wheel...?

var str = 'xPasswords%20do%20not%20match';
console.log( decodeURI(str) ); // "xPasswords do not match"

Javascript has both decodeURI and decodeURIComponent which differ slightly in respect to their encodeURI and encodeURIComponent counterparts - you should familiarise yourself with the documentation.

Use the global flag in regexp:

var replaced = str.replace(/%20/g, " ");
                                ^

using unescape(stringValue)

var str = "Passwords%20do%20not%20match%21";
document.write(unescape(str))

//Output
Passwords do not match!

use decodeURI(stringValue)

var str = "Passwords%20do%20not%20match%21";
 document.write(decodeURI(str))

Space = %20
? = %3F
! = %21
# = %23
...etc

This method uses the decodeURIComponent() function, which is the most reliable one.

var str = "Passwords%20do%20not%20match%21";
var replaced = decodeURIComponent(str);  // "Passwords do not match"

console.log(replaced);

Here are some characters it replaces:

%20 to <space>
%3F to "?"
%21 to "!"
%23 to "#"
etc...

There's a good example of this at the Mozilla docs [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI].

Please note that if you ONLY want to replace "%20", use the replaceAll function instead:

var str = "Passwords%20do%20not%20match";   
var replaced = str.replaceAll("%20", " "); // "Passwords do not match";

console.log(replaced)

You can just use .replaceAll()

If you need to remove white spaces at the end then here is a solution: https://www.geeksforgeeks.org/urlify-given-string-replace-spaces/

const stringQ1 = (string)=>{
  //remove white space at the end 
  const arrString = string.split("")
  for(let i = arrString.length -1 ; i>=0 ; i--){
    let char = arrString[i];
    
    if(char.indexOf(" ") >=0){
     arrString.splice(i,1)
    }else{
      break;
    }
  }

  let start =0;
  let end = arrString.length -1;
  

  //add %20
  while(start < end){
    if(arrString[start].indexOf(' ') >=0){
      arrString[start] ="%20"
      
    }
    
    start++;
  }
  
  return arrString.join('');
}

console.log(stringQ1("Mr John Smith   "))

本文标签: Javascript replace all quot20quot with a spaceStack Overflow