-1

I'm having some trouble trying to figure this out,

basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.

Now I've tried to do something like this..

if(string.includes('%20')) {
   const arr = str.split('%20');
}

which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.

Any help would be appreciated.

Smokey Dawson
  • 7,596
  • 15
  • 68
  • 133

3 Answers3

5

Using regex,

str.replace(/%20/g, ' ');
Moumen Soliman
  • 1,544
  • 1
  • 13
  • 19
3

Just use join:

str.split('%20').join(" ")
Krzysztof Atłasik
  • 20,861
  • 6
  • 47
  • 70
3

let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);

replace

Cabrera
  • 31
  • 3