var textTitle = "this is a test"
var result = textTitle.replace(' ', '%20');
But the replace functions stops at the first instance of the " " and I get the
Result : "this%20is a test"
Any ideas on where Im going wrong im sure its a simple fix.
var textTitle = "this is a test"
var result = textTitle.replace(' ', '%20');
But the replace functions stops at the first instance of the " " and I get the
Result : "this%20is a test"
Any ideas on where Im going wrong im sure its a simple fix.
You need a /g
on there, like this:
var textTitle = "this is a test";
var result = textTitle.replace(/ /g, '%20');
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.
The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
Would be better to use a regex here then:
textTitle.replace(/ /g, '%20');
Try using a regex instead of a string for the first argument.
"this is a test".replace(/ /g,'%20')
// #=> "this%20is%20a%20test"