-3

Possible Duplicate:
Why does javascript replace only first instance when using replace?
How do I replace all occurrences of “/” in a string with “_” in JavaScript?

I want to replace every - in a sentence but it only replace the first -. Here is my code:

var string = 'this-is-a-line-of-words';
alert(string.replace('-', '/'));​

Why does it only replace the first character I want to replace? jsFiddle demo.

Thanks in advance.

Community
  • 1
  • 1
Airikr
  • 5,802
  • 12
  • 53
  • 104

2 Answers2

2

Use a global regex:

string.replace(/-/g, '/')
elclanrs
  • 89,567
  • 21
  • 132
  • 165
1

Please use string.replace(/-/g, '/'). And check this doc please.

bhuang3
  • 3,283
  • 2
  • 16
  • 16
  • Thanks :) I didn't Googled so well this time because I was desperate and even tired after hard work :/ Sorry – Airikr Nov 28 '12 at 22:34