182
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.

sajadre
  • 1,141
  • 2
  • 15
  • 30
Yardstermister
  • 2,041
  • 3
  • 15
  • 12

7 Answers7

296

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.

Jonathan
  • 2,700
  • 4
  • 23
  • 41
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • For replacing a pipe, see [this](http://stackoverflow.com/questions/7795749/replace-pipe-and-comma-with-regex-in-javascript). – craned Dec 16 '15 at 17:46
10
textTitle.replace(/ /g, '%20');
Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
8

The same, if you need "generic" regex from string :

const textTitle = "this is a test";
const regEx = new RegExp(' ', "g");
const result = textTitle.replace(regEx , '%20');
console.log(result); // "this%20is%20a%20test" will be a result
    
Nigrimmist
  • 10,289
  • 4
  • 52
  • 53
5

From w3schools

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');
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
4

Try using a regex instead of a string for the first argument.

"this is a test".replace(/ /g,'%20') // #=> "this%20is%20a%20test"

J. Holmes
  • 18,466
  • 5
  • 47
  • 52
3

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

Subham Debnath
  • 689
  • 8
  • 9
-5

Try using replaceWith() or replaceAll()

http://api.jquery.com/replaceAll/

amfeng
  • 1,065
  • 5
  • 17
  • 1
    Didn't he ask for a JQuery version? I don't see a .replace() in the JQuery API - that's vanilla Javascript. – amfeng Jul 09 '10 at 17:05
  • 2
    @Nick Are you jealous or what? :) – Nikita Rybak Jul 09 '10 at 17:05
  • 4
    @Nikita - No...this isn't at all relevant to the question. the OP is mistaken including jQuery in there at all, this is vanilla JavaScript, having *nothing* to do with jQuery or it's replace methods... – Nick Craver Jul 09 '10 at 17:06
  • @Nick: Fair enough I guess; I'm just trying to give him what he asked for. – amfeng Jul 09 '10 at 17:09
  • 2
    @afeng - I understand that, try and read the entire question though, not the title only...the question is about the `.replace()` string operator, no jQuery involved, it's a common mix-up, unfortunately. – Nick Craver Jul 09 '10 at 17:11
  • This is only answer that helped me. I'm not great with JavaScript so I couldn't work out how ' ' became / /g Presumably / is just an escaped space character, but how do I adapt that for textTitle.replace('abc', 'def'); – Craig Aug 31 '23 at 05:41