JavaScript .replace only replaces first Match [duplicate]
You need a /g
on there, like this:
var textTitle = "this is a test";var result = textTitle.replace(/ /g, '%20');console.log(result);
You can play with it here, the default .replace()
behavior is to replace only the first match, the /g
modifier (global) tells it to replace all occurrences.
Try using a regex instead of a string for the first argument.
"this is a test".replace(/ /g,'%20')
// #=> "this%20is%20a%20test"