4

How to replace charecter "/" to "-" from string using jquery.

I want as below:

06/01/2013 to 06-01-2013

In My Case:

I am taking a value from input box

<input id="from-date" value="06/01/2013" name="from-date"/>

fdate = $("#from-date").val();
fdate.replace('/','-');
console.log(fdate);

but it return same string(06/01/2013).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
vin
  • 562
  • 1
  • 8
  • 24
  • 1
    Your problem is that the `replace` function returns a value. And when you create questions please look at the duplicates that are suggested .... – Manse Feb 06 '13 at 15:19
  • thanks I did same as you are telling but It was not work for me as there is my some my mistake. So I post my actual issue here. thanks again to suggest me. – vin Feb 06 '13 at 15:43

1 Answers1

5

You don't need jQuery but the standard replace function with the proper regex syntax :

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

Note that replace doesn't change the string you pass but returns a new one. Note also you need to pass the g flag so that all occurrences are replaced.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758