77

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"
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
user3109009
  • 805
  • 1
  • 6
  • 6
  • 4
    You can use `window.decodeURIComponent()`, it will convert more than just '%20' though. – Matt Greer Dec 26 '13 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 Dec 27 '13 at 00:01

7 Answers7

162

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)
Community
  • 1
  • 1
isset
  • 2,093
  • 1
  • 13
  • 14
81

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.

Community
  • 1
  • 1
Emissary
  • 9,954
  • 8
  • 54
  • 65
  • 8
    I'm surprised to see that this is the only answer not trying to actually replace things in the string. This is the proper way to handle this situation. – Corey Ogburn Dec 26 '13 at 23:30
  • It doesn't work in url hash in react . I tried both encode and decode none of these effect in browser url . window.location.hash = decodeURI(tab); http://localhost:5173/#new%20abc%20abc%20abc – ThanHtutZaw May 10 '23 at 04:48
14

Use the global flag in regexp:

var replaced = str.replace(/%20/g, " ");
                                ^
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
9

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
Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
  • 1
    `decodeURIComponent` will work as expected, the use of `unescape` should not be recommended - please refer to the giant red warning at the top of the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape) for more details. – Emissary Mar 14 '20 at 08:44
7

This method uses the decodeURIComponent() (See edit below) method, which is the best one.

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

Here it how it works:

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

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

Edit: decodeURIComponent works better, look at the example.

Tiago Rangel
  • 1,159
  • 15
  • 29
0

If you want to use jQuery you can use .replaceAll()

Daut
  • 2,537
  • 1
  • 19
  • 32
-2

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   "))
ASHISH R
  • 4,043
  • 1
  • 20
  • 16