How can I replace a character from a string using jquery?

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

I want like below:

06/01/2013 to 06-01-2013

      

In my case:

I am taking value from input field

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

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

      

but it returns the same string(06/01/2013)

.

+3


source to share


1 answer


You don't need jQuery, but the standard replace function has the correct regex syntax:

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

      



Note that it replace

does not change the string you pass, but it returns a new one. Note that you need to pass the flag g

so that all occurrences are replaced.

+4


source







All Articles