Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  193] [ 5]  / answers: 1 / hits: 163411  / 14 Years ago, fri, july 9, 2010, 12:00:00
var textTitle = "this is a test"
var result = textTitle.replace(' ', '%20');

But the replace functions stop at the first instance of the " " and I get the


Result: "this%20is a test"


Any ideas on where I'm going wrong I'm sure it's a simple fix.


More From » regex

 Answers
10

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.


[#96288] Tuesday, July 6, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ramiro

Total Points: 431
Total Questions: 96
Total Answers: 105

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
ramiro questions
Thu, May 7, 20, 00:00, 4 Years ago
Tue, Apr 28, 20, 00:00, 4 Years ago
Sun, Feb 16, 20, 00:00, 4 Years ago
;