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.
This question is related to
javascript
regex
replace
Try using a regex instead of a string for the first argument.
"this is a test".replace(/ /g,'%20')
// #=> "this%20is%20a%20test"
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');
The same, if you need "generic" regex from string :
const textTitle = "this is a test";_x000D_
const regEx = new RegExp(' ', "g");_x000D_
const result = textTitle.replace(regEx , '%20');_x000D_
console.log(result); // "this%20is%20a%20test" will be a result_x000D_
_x000D_
textTitle.replace(/ /g, '%20');
For that you neet to use the g flag of regex.... Like this :
var new_string=old_string.replace( / (regex) /g, replacement_text);
That sh
Try using replaceWith()
or replaceAll()
Source: Stackoverflow.com