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

0

2 Answers 2

2

Use a global regex:

string.replace(/-/g, '/') 
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for a fast answer! I'll accept it when I can :)
1

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

1 Comment

Thanks :) I didn't Googled so well this time because I was desperate and even tired after hard work :/ Sorry

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.