-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
  • 6,258
  • 15
  • 59
  • 110

2 Answers2

2

Use a global regex:

string.replace(/-/g, '/')
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

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

bhuang3
  • 3,493
  • 2
  • 17
  • 17
  • 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