3

Possible Duplicate:
Fastest method to replace all instances of a character in a string = Javascript
How to replace all points in a string in JavaScript

I have a string 2012/04/13. I need to replace the / with a -. How can i do this ?

var dateV = '2012/04/13';
dateV= dateV.replace('/','-');

It only replaces the first / and not all / in the string (2012-04/13). What should i do to correct this ?

2
  • 1
    Look at the g flag for regexes. Commented Jul 25, 2012 at 0:14
  • Possible duplicate of stackoverflow.com/q/10507770/1331430 (good reference as well), but I'm voting to close with the first as duplicate. Commented Jul 25, 2012 at 0:17

2 Answers 2

4

You need to do a global regex replace using the global regex option. This should work for you:

var dateV = '2012/04/13';
var regex = new RegExp("/", "g"); // "g" - global option
dateV = dateV.replace(regex, "-");
console.log(dateV);

Sign up to request clarification or add additional context in comments.

Comments

0

Use

dateV= dateV.replace(/\//g,'-');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.