-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
  • 8,827
  • 19
  • 77
  • 152

3 Answers3

5

Using regex,

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

Just use join:

str.split('%20').join(" ")
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
3

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

replace

Cabrera
  • 31
  • 3